I\'m trying to dynamically resize my textview but getlinecount() method always returns me 0 even after settext() and invalidate(). I\'m using the following code:
In order to fix this issue apply the following lines:
textView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (textView.getLineCount() > 1) {
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
The OnGlobalLayoutListener will be called after every change to the TextView (after measuring it, drawing it, ..). Here you can catch changes to your TextView before it'll be drawn to the screen and do whatever you need with it.
The last line in the code is to remove the listener, it's important since we don't want to continue catching each layout change.