How to disable the TextView maxLines programmatically?

AndroidTextview

Android Problem Overview


I'm having a hard time reseting the maxLines attribute of a TextView programmatically.

Just tried setting to 0 and it doesn't work. -1 crashes the application. I could use a simpler workaround and set the maxLines to 5000 but I don't want to do that.

Any ideas how to do that?

UPDATED

Well, I've found one problem.. I've set the Ellipsize as well... I'm just going to use the following workaround:

TextView questionDetail = (TextView) mQuestionDetailHeader.findViewById(R.id.desc);

questionDetail.setText(mCurrentQuestion.getQuestion());
questionDetail.setMaxLines(Integer.MAX_VALUE); //As in the android sourcecode
questionDetail.setEllipsize(null);

Android Solutions


Solution 1 - Android

As there isn't yet an approved answer - the proper way to reset the maxlines property of the TextView is:

textView.setMaxLines(Integer.MAX_VALUE);

As per Valdemar's comment and this stackoverflow answer. Using -1 will cause an ArrayIndexOutOfBoundsException.

Keep in mind only END and MARQEE setEllipsize() settings will be respected for maxlines >= 2 according to the documentation:

> If setMaxLines(int) has been used to set two or more lines, END and > MARQUEE* are only supported (other ellipsizing types will not do > anything).

Solution 2 - Android

For setting the maxLines for a text use mTextView.setMaxLines(0) or you have to programmatic-ally measure of the height text and multiply with the number of max line The result should set as the height of the textView

Solution 3 - Android

if you want to have just a single line , then why don't you use:

    txtView.setSingleLine(true);

Solution 4 - Android

The -1 should not crash your application. This actually what is used inside TextView by default:

case com.android.internal.R.styleable.TextView_maxLines:
            setMaxLines(a.getInt(attr, -1));
            break;

This piece of code shows, that when android:maxLines is not specified, then code uses -1 to set the value via setMaxLines() function.


I also made test application to verify my conclusions. And it works fine without crashing:

public class HelloWorld extends Activity
{			
			
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        
        TextView text = (TextView)findViewById(R.id.text);
        text.setMaxLines(-1);    
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionValdemarView Question on Stackoverflow
Solution 1 - AndroidalexgophermixView Answer on Stackoverflow
Solution 2 - AndroidbladeXView Answer on Stackoverflow
Solution 3 - AndroidParesh MayaniView Answer on Stackoverflow
Solution 4 - AndroidinazarukView Answer on Stackoverflow