James
James

Reputation: 1009

TextView.setMaxLines not working?

In my app I have a screen where I display some text and then a photo. The text is variable in length (sometimes none at all, sometimes a lot), so I wanted to have it set up so the text never takes up more than a few lines (but can be scrolled) leaving enough room for the image below.

My view component for this part is created programatically, and I've adjusted the code to have the following (currently in my text-setting method, but the same thing happens if it's in the initial view-create code)

public void SetDescription(String description)
{
    mTxtDescription.setText(Html.fromHtml(description));
    mTxtDescription.setClickable(true);
    mTxtDescription.setMaxLines(5);
    mTxtDescription.setLines(5); //this makes no difference either!
    mTxtDescription.setSingleLine(false);
    mTxtDescription.setScrollbarFadingEnabled(true);
    mTxtDescription.setScrollBarStyle(VERTICAL);
    mTxtDescription.setMovementMethod(ScrollingMovementMethod.getInstance());
    mTxtDescription.invalidate(); //adding this made no difference...
}

However it doesn't work- long text still fills the whole screen and the image has vanished due to being pushed down to a height of 0. How can I get the text to never be more than 5 lines?

Upvotes: 4

Views: 5064

Answers (3)

Ulf Yngwe
Ulf Yngwe

Reputation: 29

The setSingleLine(false) seemes to reset the setMaxLines command. Try to move the setSingleLine command before the setText. That worked for me.

Upvotes: 1

Vishwanath.M
Vishwanath.M

Reputation: 6317

The below code is working fine for me

   txt = (TextView)findViewById(R.id.textview);
   txt.setMaxLines(5);
   txt.setMovementMethod(new ScrollingMovementMethod());

   txt.setScrollContainer(true);
   txt.setText("Example Text");
   txt.setTextColor(Color.WHITE);
   txt.setScrollbarFadingEnabled(true);

in xml inside textview

   android:scrollbars="vertical"

Upvotes: 0

Rich Schuler
Rich Schuler

Reputation: 41972

Try removing the call to setSingleLine. And use setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE). It'd also put this call before the setMaxLines and setLines call to be sure.

Note: setLines overrides the settings of setMaxLines and setMinLines.

The TextView has many issues surrounding the various calls to how it should display multiple, ellipses, etc.

Upvotes: 4

Related Questions