2

I need to get height and width of the textView's text. Yes there is a method called textView.getTextSize(), but I don't understand what that size means: a height, a width or something else? I need the exact width and height values (it will be good, if values will be in pixels not sp).

Ruben Kubalyan
  • 288
  • 3
  • 11

3 Answers3

2

One approach could be with the use of getPaint() and getTextBound()

String text = (String) textView.getText();
Rect textBound = new Rect();
textView.getPaint().getTextBounds(text,0,text.length(),textBound);
int textHeight = textBound.height();
int textWidth = textBound.width();

I am not entirely sure of the height and width being in pixel. Based on this Q&A in regards to RectF, I believe Rect as well should use pixels.

Kalai
  • 503
  • 1
  • 3
  • 12
  • Interesting experiment: I tried to get values with Log.d(), the first value is the height, which I tried to get with the code, you wrote and the second value is the same height but this time by textView.getTextSize(). I think that textView.getTextSize() returns value of height, cause another person who answered, said that. But the strange thing is: that two returned values are not the same. the first == 31 and the second == 39.0. I read the docs and there is said that textView.getTextSize() returns value in pixels. The same thing is written for Rect. So what's going on? O_O – Ruben Kubalyan Dec 03 '20 at 23:40
  • 1
    The difference seem to be similar to the difference between `measureText()` and `getTextBound()` width as shown in this [answer](https://stackoverflow.com/questions/7549182/android-paint-measuretext-vs-gettextbounds/7579469#7579469). – Kalai Dec 04 '20 at 03:54
0

getTextSize() returns the TextView's line height in pixels.

I think what you're looking for is textView.getLayout().getHeight() and textView.getLayout().getWidth(), although the Layout can be null if the TextView changed recently.

npace
  • 4,218
  • 1
  • 25
  • 35
  • for the "getTextSize() returns the TextView's height in pixels." thanks. That's a great info. But the other part you misunderstood. No I don't need the values of the View, I need only values of the text of the View. Only that. – Ruben Kubalyan Dec 03 '20 at 17:30
0

getTextSize() returns the size of the text set on the which is effectively the font size of the TextView, you can determine this by looking at the setTextSize method: https://developer.android.com/reference/android/widget/TextView#setTextSize(int,%20float)

To figure out the height/width of a TextView you could use:

https://developer.android.com/reference/android/view/View#getMeasuredHeight() https://developer.android.com/reference/android/view/View#getMeasuredWidth()

This is assuming that the view has been measured and laid out.

As for useful conversions that could be helpful here refer to: How to convert DP, PX, SP among each other, especially DP and SP?

Whenever unsure about what a method does, check the docs and also check other methods to figure out what it does, like you can figure out what getTextSize returns by looking at setTextSize.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45