0

I have a layout that looks as follows:

|User Name      |      |
|[email protected] | info |

And I need to set user's first name and second name if it fits into TextView and only first name if first name and second name is too long for current screen.

For example for "Tom Smith":

|Tom Smith      |      |
|[email protected] | info |

and for "Tom VeryVeryVeryLongSurname":

|Tom            |      |
|[email protected] | info |

How can I get a name size with specified text size and compare it with TextView width?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="bottom"/>
</LinearLayout>
AndroidDev
  • 71
  • 4

1 Answers1

0

See TextPaint#getTextBounds method http://developer.android.com/reference/android/graphics/Paint.html#getTextBounds%28char[],%20int,%20int,%20android.graphics.Rect%29

And other from the TextPaint and Paint classes

Paint.html#measureText seems to be even more matching what you are looking for. http://developer.android.com/reference/android/graphics/Paint.html#measureText%28java.lang.CharSequence,%20int,%20int%29

Anyway look at the android.graphics.Paint class.

Tomasz Gawel
  • 8,379
  • 4
  • 36
  • 61