44

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 !

Jonik
  • 80,077
  • 70
  • 264
  • 372
Jerec TheSith
  • 1,932
  • 4
  • 32
  • 41

7 Answers7

89

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())
JPM
  • 9,077
  • 13
  • 78
  • 137
Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143
  • sorry but it doesn't change anything. Would it have something to do with the character limit of a textView ? – Jerec TheSith Oct 16 '12 at 09:55
  • 1
    Maybe, just try to decrease the maxLines, and add alot of text. I doesn't have my enviroment available now, so its just a random guess. – Tobias Moe Thorstensen Oct 16 '12 at 10:02
  • 1
    Add `android:minLines="1"` too – tar Jun 06 '14 at 01:51
  • Why did you write: "[...] you must also add: `android:minLines="1"`? Android's documentation doesn't state that both properties must be set and my app doesn't need to have `minLines` set. In what cases is it required? – Erik Nov 14 '17 at 08:13
  • Didn't worked for me, but after adding `android:ellipsize="marquee"` it's working now.. – Shailendra Madda Nov 27 '17 at 09:25
3

Find your textview by id:

TextView myTextBox=(TextView)findViewById(R.id.textBox);

Now use the setMaxLines function and assign the number of lines you require in it,say 20.

myTextBox.setMaxLines(20);

This limits your text box to show 20 lines only.

kapilgm
  • 1,620
  • 1
  • 15
  • 22
2

put your text view with in scroll view and set fixed height of scroll view.

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="120dip" >

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

set properties as per your need

Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113
2
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="120dip" >

<TextView
    android:id="@+id/tv_addesc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxLines="12"
    android:scrollbars="vertical"
    android:textColor="#FFFFFFFF"
    android:textSize="15sp" />
 </ScrollView>
Rauf
  • 620
  • 1
  • 8
  • 20
2

Try using this.

android:maxLines="12"
android:ellipsize="end"

In the end it add ... if extra text available.

You can also use

android:ellipsize="marquee" 

It will reduce extra text from end that is not like a word.

1

hey try setting the singleline property to false. Just see if it works.

gnat
  • 6,213
  • 108
  • 53
  • 73
bostan
  • 373
  • 2
  • 8
  • 1
    it doesn't change anything, but even `android:singleLine="true"` doesn't work too – Jerec TheSith Oct 16 '12 at 10:03
  • 1
    Dont give a static height to scrollView...give it wrap content and then add the textview to a linear or relative layout..and then you can give a static height to this layout.. – bostan Oct 16 '12 at 10:15
0

I tried as said in accepted answer and others but didn't worked for me, but after adding android:ellipsize="marquee" it's working now..

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138