detect clipping in android TextView

I have a TextView in my android application that has a set width on it. It's currently got a gravity of "center_horitonzal" and a set textSize (9sp). I pull values to put on this label from a sqlite database, and some of the values are too large to fit in the TextView at the current textSize.

Is there a way to detect that the text inside a TextView is going to be clipped? I'd like to detect this, and lower the font until it fits. There is a nice property in the iPhone UILabel that handles this called "adjustToFit" (which also has a minimum font size), and I'm basically trying to emulate that.

Here's an example of the TextView that I'm working with:

<TextView android:id="@+id/label5" android:layout_width="62px"
            android:layout_height="wrap_content" android:layout_x="257px"
            android:layout_y="169px" android:textSize="9sp" 
            android:typeface="sans" android:textStyle="bold"
            android:gravity="center_horizontal" android:lines="1" />
14.10.2009 01:16:00

I found a way to measure the width of text using the TextView's Paint object, and lower it until it fit in the size I needed. Here's some sample code:

    float size = label.getPaint().measureText(item.getTitle());
    while (size > 62) {
        float newSize = label.getTextSize() - 0.5f;
        size = label.getPaint().measureText(item.getTitle());
14.10.2009 01:29:08

Another way to do this (which might be equivalent) is something like this:

        TextView title = new TextView(context) {
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                int textsize = 30;
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                while (getMeasuredHeight() > MeasureSpec.getSize(heightMeasureSpec)) {
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

Just a warning that this seems to work, but I just threw it together - might be some edge cases in ways the TextView can be measured.

26.01.2010 21:59:55
I should add here that after using this a bit it only handles certain layout situations properly, so this should only be used as a starting point. If I ever get a more general version working I'll post it here.
Matt Hall 30.01.2010 21:33:28

I wrote this function to trim off letters from the end of the text until it meets a certain width requirement.

The 0.38 function is setting the proportion of the screen I want to fill with this text, in this case, it was 38% since I wanted it to cover ~40% including padding. Worked for the cases I've tested it with.

Using this code to call the function below

DisplayMetrics metrics = new DisplayMetrics();

double maxWidth = (double)metrics.widthPixels*0.38 - 1;
TextPaint painter = station.getPaint();

String left = item.getLocationName();
left = trimToLength(painter, left, maxWidth);


This is the function

public String trimToLength(TextPaint painter, String initialText, double width)
    String message = initialText;
    String output = initialText;

    float currentWidth = painter.measureText(output);
    while (currentWidth > width)
        message = message.substring(0, message.length()-1);

        output = message + "...";

        currentWidth = painter.measureText(output);

    return output;
3.04.2011 00:24:24
The string operations are rather awkward. One of the first things to avoid in any Java implementation is to do such String operations (substring, +) in a loop... use StringBuilders instead.
Zordid 1.02.2015 10:40:20
When trying to trim lengths of strings, you're not going to be dealing with super long strings, and if you are, this algorithm is far from optimal even if you used a StringBuilder
Kurru 4.02.2015 21:39:54