Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset Android textview maxlines

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?

like image 323
jexcy Avatar asked Mar 24 '11 00:03

jexcy


2 Answers

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

like image 60
Valdemar Avatar answered Nov 05 '22 04:11

Valdemar


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);
    }
}
like image 6
beshkenadze Avatar answered Nov 05 '22 04:11

beshkenadze