androidandroid-layouttextviewlayoutparams

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


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 ?


Solution

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