1

I used this code:

Rect bounds=new Rect();
tv.getPaint().getTextBounds(text,0, text.length(),bounds);
float textWidth=bounds.width();
float textHeight=bounds.height();

and it works well horizontally... also vertical size is ok... but the text is painted in a position lower than what I expect...

these links was not useful:

get text height

auto scale textview text to fit within bounds

what should I change?

view and its text

Paraskevas Ntsounos
  • 1,653
  • 2
  • 14
  • 32

1 Answers1

0

Just set your layout params to WRAP_CONTENT for the height. If your text is in a LinearLayout it would look like this:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
myTextView.setLayoutParams(params);

If you're using a XML layout just set your TextView's layout_height to wrap_content. If the text is still painted lower than you expect check whats the padding set to the TextView.

Veselin Todorov
  • 271
  • 3
  • 9