8

I have some text that will be put in a TextView. I did that using setText().

Now I need to find the number of lines or height the text occupies in the TextView. I tried using getHeight(), but it always returns 0.

Is there anyway to get the height of the text present in the TextView?

hichris123
  • 10,145
  • 15
  • 56
  • 70
Siva Kumar
  • 893
  • 4
  • 14
  • 28
  • 1
    You can't get the height of a view until it has been rendered, that's why you always get 0 returned if you're checking the height just after you've set the text. Why do you need to know the height? Please explain more about what you are trying to achieve. – John J Smith Feb 06 '11 at 11:30
  • actually i got to develop an application that displays the text as well as images(just like a ebook reader) page by page(I mean it shouldnt contain any scrollbars).. so, when i have a bulk of text to be put, i need to know how much the text is occupying in the screen so that it fits in screen.. – Siva Kumar Feb 06 '11 at 13:34

2 Answers2

13

Because this size is known only after the layout is finished, one way is to create a custom text view and use onSizeChanged method. Another way is this:

    final TextView tv = (TextView) findViewById(R.id.myTextView);
    ViewTreeObserver vto = tv.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 
            Toast.makeText(MyActivity.this, tv.getWidth() + " x " + tv.getHeight(), Toast.LENGTH_LONG).show();
            tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } 
    }); 

This code I've found here: How to retrieve the dimensions of a view?

Community
  • 1
  • 1
Lumis
  • 21,517
  • 8
  • 63
  • 67
  • The prblem with the above code is that .. the height of the textview can be achieved only at the end of the code .. i am unable to get it in the middle !! I took a variable to store the height and initialised the variable with tv.getHeight() in onGlobalLayout().. but the varible's value is always 0 till the end .. at the end it changes to the height of the text... – Siva Kumar Feb 07 '11 at 07:27
  • This code helped me on finding TextView's "width" and "height". – Can Uludağ Jul 15 '15 at 11:33
7

As John pointed out, you won't be able to get the height immediately after setText. I'm not sure using getHeight() on the TextView itself will help you much. The height of the view is not only dependant on the height of the visible text in the view, but also on the viewgroup/layout the TextView resides in. If the viewgroup tells the TextView to maximize height, getHeight() will get you nowhere even if you wait until after the text is rendered.

I see a number of ways that this could be done:

  1. Subclass TextView and overwrite the onSizeChanged method. In there, call supers onSizeChanged, then get the number of lines within the TextView by getLineCount() and the height per line with getLineHeight(). This might or might not be better than using getHeight(), depending on your layout or whatnot.
  2. Don't use the Textviews dimensions. Get the TextViews Paint with TextView.getPaint(), and then calculate the width and height from

    Rect bounds; paint.getTextBounds(text, 0, text.length(), bounds);

You'll now have the dimensions in bounds. You can now work with paint.breakText to see how much text you'll fit on one line. Probably too much hassle and not guaranteed (to my untrained eye) to be the same logic as used by TextView.

Kevin Read
  • 2,173
  • 16
  • 23
  • Kread .. can u give some example how to subclass the TextView so that the TextView object present in the main class can be used in the subclass – Siva Kumar Feb 07 '11 at 06:58
  • Well, you have public class MyText extends TextView { OVERRIDE METHODS HERE } and in the main class you do a "MyText textView = new MyText(this);" instead of "TextView textView = new TextView(this);". If you want to use XML layouts instead, I recommend reading http://developer.android.com/guide/topics/ui/custom-components.html (Actually read that too even for creating stuff in Java). – Kevin Read Feb 07 '11 at 10:08
  • Thnks .. but onSizeChanged method is getting called at the end !! so the getLineCount cant be used in the middle of the application !I mean .. if i want to initialize a variable with textview.getLineCount(),the variable value is 0 till the end even after setting the text – Siva Kumar Feb 07 '11 at 10:25