How to find the Text Area(Height/Width) of TextView programmatically in android

BST Kaal picture BST Kaal · Jun 23, 2014 · Viewed 25.4k times · Source

I have an EditText, a Button and a TextView. On clicking the button, textview shows the text written in edittext. Is it possible to find the size of textview occupied depending upon text. i.e. If It has three characters "abc", what is width now, if it has 5 characters like "abcde" , then what is the width ?

Answer

pigeongram picture pigeongram · Jun 23, 2014
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text,0,text.length(),bounds);
int height = bounds.height();
int width = bounds.width();

or

textView.setText("bla");
textView.measure(0, 0);
textView.getMeasuredWidth();
textView.getMeasuredHeight();