2

I want to check the no of lines in my text view from the string I am going to pass to the test view. I have added android:maxLines = "2".... If it exceeds two i need to show/hide a view....How can I achieve this?

ydnas
  • 519
  • 1
  • 12
  • 29

2 Answers2

5

Use a thread to count the number of lines,

 textView.setText("Text Here");
    textView.post(new Runnable() {

        @Override
        public void run() {

            Log.v("Line count: ", textView.getLineCount()+"");
        }
    });

If you want to limit the number of lines in TextView from xml then use android:maxLines

Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
3

I believe the TextView has a method getLineCount which you can use to get the number of lines. You can compare that function's return value with maxLines and show/hide if the limit is reached.

See also: TextView.getLine()

And: TextView - maxLines attribute

RichS
  • 913
  • 4
  • 12