Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit numbers of lines in TextView

I have a scrollable textView, and I want to limit the number of lines displayed, however xml properties are not working :

<TextView    android:id="@+id/tv_addesc"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:scrollbars="vertical"    android:maxLines="12"    android:textColor="#FFFFFFFF"    android:textSize="15sp" /> 

the number of lines displayed is 50 and there are 900 chars in the text.

How can I limit the number of lines displayed and make it scrollable ?

Edit : I tested with 846 lines and 15824 chars, the whole text is displayed regardless of different properties set.

Edit : there was a second component besides the textView, when i removed it it worked, so I will find a workaround. Thank you !

like image 650
Jerec TheSith Avatar asked Oct 16 '12 09:10

Jerec TheSith


People also ask

How do I limit text in TextView?

you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound. Better than counting the text length or the word cound a better way would be to use the "maxLines" attribute along with "ellipsize" attribute to attain the desired effect.

How do you check if a TextView's text exceeds the max lines?

text_view. post(new Runnable() { @Override public void run() { if (text_view. getLineCount() < text_view. getMaxLines()) { // do stuff } } });

What is Ellipsize in TextView?

Android Ellipsize Android TextView ellipsize property Causes words in the text that are longer than the view's width to be ellipsized ( means to shorten text using an ellipsis, i.e. three dots …) instead of broken in the middle to fit it inside the given view.


1 Answers

You just have to set a number of lines in your TextView like this:

android:maxLines = "10" 

and you must also add:

android:minLines="1" 

The rest of this not necessary if you are not using scrolling

and a property which says that this TextView should be scrollable vertically:

android:scrollbars = "vertical" 

And in your Java-code:

yourTextView.setMovementMethod(new ScrollingMovementMethod()) 
like image 140
Tobias Moe Thorstensen Avatar answered Sep 29 '22 20:09

Tobias Moe Thorstensen