1

I have a TextView with specific font and font size set. I want to determine if a multiline text will fit into this TextView (the TextView is set to match_parent for both width and height). Is there any reasonable way of doinf it?

Thanks

cubesoft
  • 3,448
  • 7
  • 49
  • 91
  • Yes. You could make use of the solution I posted [here](https://stackoverflow.com/a/32096884/3290339). – Onik Jun 17 '19 at 19:01

2 Answers2

1

You can measure the bounds of your text using this approach:

String yourText = "...";
Rect bound = new Rect();
textView.getPaint().getTextBounds(yourText , 0, yourText.length(), bounds);

Then you can get width and height of text for the giving textview:

bounds.width() or bounds.height();
Alexei Artsimovich
  • 1,074
  • 7
  • 15
0

If you put your TextView inside a ScrollView, you will know it will fit 100% :)

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Ty Juan" />
</ScrollView>
Juan Sancho
  • 321
  • 1
  • 7