Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a multi line TextView with max height and internal scroll

I want to limit the amount of space a TextView can take in the screen with the following rules:

  • If the text is short (lets say 140 characters or 2 lines) let it take as much room as it need but no more.
  • If the text is long, i.e. a paragraph, limit the max height of the view (with some kind of maxHeight or with a maximum number of lines)

Here go some screenshots explaining valid and invalid results:

Short Text:

enter image description here

Medium Text:

enter image description here

Internal Scroll:

enter image description here

Blank spaces:

enter image description here

Excessive length:

enter image description here

like image 507
Addev Avatar asked Oct 10 '14 08:10

Addev


1 Answers

Try like this.

XML

<TextView
   android:id="@+id/descTxtView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:maxLines="5"
   android:scrollbars="vertical"
   android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&apos;s standard dummy text ever since the 1500s."
   android:textColor="#232e3b"
   android:typeface="sans" />

in JAVA

descTxtView= (TextView) findViewById(R.id.descTxtView);
descTxtView.setMovementMethod(new ScrollingMovementMethod());
like image 53
Kaushik Avatar answered Sep 25 '22 16:09

Kaushik