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?
Asked
Active
Viewed 3,117 times
2
-
You can check it only if your TextView contains `\n` otherwise I dont think it is possible. – Lucifer Apr 04 '14 at 05:55
-
Also, I think the android:maxLines property will **coerce** the number of lines to 2, so I don't think it will be possible to exceed. Maybe I'm wrong. – RichS Apr 04 '14 at 06:00
-
use @android:ellipsize = "end" – Sandeep Tiwari Apr 04 '14 at 06:05
2 Answers
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
-
What if it returns the exact number of lines as the max number? In this case, it can either be enough or not... – android developer Nov 04 '18 at 13:57
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()

RichS
- 913
- 4
- 12