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

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


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();


Please try this:

public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    TextView edit = (TextView) findViewById(R.id.edit);    edit.setTextSize(20);           edit.setText("Hello, world");           edit.measure(0, 0);    int width = edit.getMeasuredWidth();    Log.w("width", width.toString());}

Before you get width, you have to measure the view / label / text edit.Please let me know if this is not working.


TextView txt = new TextView(mContext);txt.setText("Some Text)";int height = txt.getLineCount() *  txt.getLineHeight();int width = txt.getWidth();