1

I want to get the height or at least the line count of my text view programatically, but the log says 0. What is wrong ? Here is my code :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_off);

    titreOff = (TextView) findViewById(R.id.offTitre);
    titreOff.setText("some text"); // displays 2 lines of text with the font size I used
    System.out.println(titreOff.getLineCount() + " and " + titreOff.getHeight());
}

Thanks for your advices

Rob
  • 15,732
  • 22
  • 69
  • 107
  • Possible duplicate of [textview.getLineCount always 0 in android](http://stackoverflow.com/questions/3528790/textview-getlinecount-always-0-in-android) – cammando Feb 06 '17 at 18:27

3 Answers3

3

In onCreate(), your TextView has not had a layout pass and so currently has a zero width and height. There are a few solutions if you would like to get the dimensions of your TextView:

  1. Add an OnGlobalLayoutListener to your TextView's ViewTreeObserver. (e.g. titreOff.getViewTreeObserver().addOnGlobalLayoutListener(listener) and handle your changes there).

  2. Override TextView and handle your special case by overriding onLayout() (in which you will have the dimensions of your TextView as parameters.

Alternatively, what is the end goal of your measurements? If you can give some specifics, there may be an easier solution than any of this.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 1
    As a warning, don't make my mistake and make layout changes within the listener -- this will cause it to re-enter the listener infinitely. If you *do* need to make layout changes within the listener, you should remove the listener within itself (i.e. `titreOff.getViewTreeObserver().removeGlobalOnLayoutListener(this)` within the `onGlobalLayout()` method. – Kevin Coppock Jul 16 '12 at 14:13
  • 1
    Option #1 does not work reliably (it's common for getLineCount to return 0 inside OnGlobalLayout despite the view containing text). You'll probably need to go with option #2. –  May 12 '14 at 16:20
2

Your app doesn't know the actual size of TextView while doing onCreate() method. The solution is to move your output here:

onWindowFocusChanged(boolean hasFocus) {
    if (!hasFocus) {
        titreOff = (TextView) findViewById(R.id.offTitre);
        System.out.println(titreOff.getLineCount() + 
        " and " + titreOff.getHeight());
    }
}
user1049280
  • 5,176
  • 8
  • 35
  • 52
  • I understand what you're trying to do but when I do that, it returns the correct value only when I exit the view and not when I enter it... – Rob Jul 16 '12 at 14:53
1

setText() doesn't immediately update the TextView, it just invalidates it. The new text will be set only on the next repaint. If you try to get the height before you will get the old one.

Instead, you should subclass TextView, override onSizeChanged() and you will be notified each time the size of your TextView changed.

Dalmas
  • 26,409
  • 9
  • 67
  • 80