1

I made a TextView scrollable if it has more than 5 lines. Now I want dynamically in my java code to detect if the same TextView is scrollable or can scroll so I can change the visibility of a view below it.

I tried:

textView.setText(description);
textView.setMovementMethod(new ScrollingMovementMethod());

if(textView.getLineCount() > 5)
{
   view.setVisibility(View.VISIBLE);
}

but the textView.getLineCount() is equal to 0.

Also tried the function canScrollHorizontally(int direction) but Google's documentation doesn't say which parameter should I use.

tvieira
  • 1,865
  • 3
  • 28
  • 45

3 Answers3

2

You getting ZERO because you try to take a result in non-UI thread.

Try

textView.post(new Runnable() {

    @Override
    public void run() {

    int lineCount = textView.getLineCount();
    if(lineCount >=5)
        textView.setVisibility(View.VISIBLE);
    }
});
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
-1

Use a thread in order to get lineCount:

@Override
public void run() {
    while(textView.getLineCount() == 0){   
    }
    textview_lineCount = textView.getLineCount(); 

}
Hana Bzh
  • 2,212
  • 3
  • 18
  • 38
-1

Assuming your TextView is a child of a ScrollView and you want to automatically scroll your TextView after it has been appended and now contains more text, than it can display, you may do the following:

TextView t = (TextView) findViewById(R.id.someTextView);
t.append("Some text");

ScrollView s;
if (s.canScrollVertically(1)) {
    logScroll(s);
}

private void logScroll(final ScrollView s) {
    s.post(new Runnable() {
        @Override
        public void run() {
            s.fullScroll(ScrollView.FOCUS_DOWN);
        }
    });
}
Neurotransmitter
  • 6,289
  • 2
  • 51
  • 38