Reset Android textview maxlines

AndroidTextview

Android Problem Overview


I want to make a TextView that is collapsable by user's touch. When the TextView collapsed, I set textView.setMaxLines(4);. How to I clear this state in my expand method? I can only think of call setMaxLines() with a value large number like 10000.

Are there better ways to implement this?

Android Solutions


Solution 1 - Android

Actually, the way android platform does that is by setting the MaxLine to Integer.MAX_VALUE.

textView.setMaxLines(Integer.MAX_VALUE);

also, if you are using Ellipsize, don't forget to set to null.

textView.setEllipsize(null);

just check how the android framework do just that ;) watch the setMaxLines(Integer.MAX_VALUE);

private void applySingleLine(boolean singleLine, boolean applyTransformation) {
	mSingleLine = singleLine;
	if (singleLine) {
		setLines(1);
		setHorizontallyScrolling(true);
		if (applyTransformation) {
			setTransformationMethod(SingleLineTransformationMethod.getInstance());
        }
       } else {
            setMaxLines(Integer.MAX_VALUE);
            setHorizontallyScrolling(false);
            if (applyTransformation) {
                 setTransformationMethod(null);
        }
       }
     }
	

You can find this in the source code of Android Open Source Project (AOSP)

https://source.android.com/source/downloading

If you do not want to download the source, you can view the source on a mirror like this one at github.

https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/widget/TextView.java

Solution 2 - Android

Try this (infoView.getLineCount()):

public void onMoreClick(View v) {
	Button btn = (Button) v;
	if(!moreSwitcher) {
		infoView.setMaxLines(infoView.getLineCount());
		infoView.setLines(infoView.getLineCount());
		moreSwitcher = true;
		btn.setText(R.string.collapse);
	}else{
		infoView.setMaxLines(5);
		infoView.setLines(5);
		moreSwitcher = false;
		btn.setText(R.string.expand);
	}
}

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
QuestionjexcyView Question on Stackoverflow
Solution 1 - AndroidValdemarView Answer on Stackoverflow
Solution 2 - AndroidbeshkenadzeView Answer on Stackoverflow