25

I need to set the TextView maximum height (either using maxHeight or maxLines). And if there are more lines of text the scrollbars should be shown. What markup should I use for this?

Initial idea was to wrap TextView with ScrollView, but ScrollView has no maxHeight attribute.

1 Answer 1

67

Layout:

<TextView android:id="@+id/text_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="false" android:maxLines="3" android:scrollbars="vertical" android:textColor="@android:color/secondary_text_dark_nodisable" > </TextView> 

Code:

TextView textView = (TextView)findViewById(R.id.text_view); textView.setMovementMethod(ScrollingMovementMethod.getInstance()); 

Note:

The android:textColor="@android:color/secondary_text_dark_nodisable" is used to avoid text fade out when textView is touched.

Sign up to request clarification or add additional context in comments.

4 Comments

When I try this, the TextView displays scrollbars but I can't actually make it scroll.
You have to execute this from code: TexView.setMovementMethod(new ScrollingMovementMethod());
I used this approach since I could not ellipsize more than two lines. Thanks.
@alex2k8 4 years later your answer helped me. Wasted an hour figuring this out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.