textview.getLineCount always 0 in android

Solution 1:

I know this question is quite old, but in case anyone comes here looking for the actual answer:

holder.text2.setText(arr2[position]);
holder.text2.post(new Runnable() {
    @Override
    public void run() {
        int lineCnt = holder.text2.getLineCount();
        // Perform any actions you want based on the line count here.
    }
});

Solution 2:

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.

Solution 3:

It should be done on UI Thread by post method.

textView.post(new Runnable() {
    @Override
    public void run() {
        Log.v("Line count: ", textView.getLineCount()+"");
    }
});