Addev
Addev

Reputation: 32233

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:

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

Upvotes: 5

Views: 7072

Answers (2)

Feng Dai
Feng Dai

Reputation: 633

It's easy if you want to limit TextView's height by line, use android:maxLines:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="5"
    android:scrollbars="vertical"
    android:requiresFadingEdge="vertical" />

In your code, write as below:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

With this code, your TextView can be scrolled vertically when the text is longer than the maxLines.

Upvotes: 3

Kaushik
Kaushik

Reputation: 6162

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());

Upvotes: 9

Related Questions