Manabu-GT / ExpandableTextView

Android's TextView that can expand/collapse like the Google Play's app description
Apache License 2.0
4.08k stars 791 forks source link

Solution for "Text is not completely shown in Expanded State (Height Calculation Issue)" #75

Open harismuneer opened 3 years ago

harismuneer commented 3 years ago

Incase of large text, the text isn't shown completely in expanded state. This is still a bug in the latest version of this repository as of today and needs to be fixed in the next release @Manabu-GT . For now, following is the solution explained clearly.

SOLUTION

In ExpandableTextView class, in the onMeasure method: Cut the line

       mTextHeightWithMaxLines = getRealTextViewHeight(mTv);

and paste it right below this line

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

Or for ease, you can directly replace your onMeasure method with the method below.

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // If no change, measure and return
        if (!mRelayout || getVisibility() == View.GONE) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        mRelayout = false;

        // Setup with optimistic case
        // i.e. Everything fits. No button needed
        mToggleView.setVisibility(View.GONE);
        mTv.setMaxLines(Integer.MAX_VALUE);

        // Measure
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //--- Previously the mTextHeightWithMaxLines was initialized here ---//       

        // If the text fits in collapsed mode, we are done.
        if (mTv.getLineCount() <= mMaxCollapsedLines) {
            return;
        }

        // Doesn't fit in collapsed mode. Collapse text view as needed. Show
        // button.
        if (mCollapsed) {
            mTv.setMaxLines(mMaxCollapsedLines);
        }
        mToggleView.setVisibility(View.VISIBLE);

        // Re-measure with new setup
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

       mTextHeightWithMaxLines = getRealTextViewHeight(mTv);

        if (mCollapsed) {
            // Gets the margin between the TextView's bottom and the ViewGroup's bottom
            mTv.post(new Runnable() {
                @Override
                public void run() {
                    mMarginBetweenTxtAndBottom = getHeight() - mTv.getHeight();
                }
            });
            // Saves the collapsed height of this ViewGroup
            mCollapsedHeight = getMeasuredHeight();
        }
    } 

This was also discussed in this issue here #5 but the solution wasn't explained clearly.

ghost commented 3 years ago

Is it AndroidX compatible now?

harismuneer commented 3 years ago

Is it AndroidX compatible now?

Some of the library versions used in the original repository are outdated now. I had to clone this repository and update the versions especially the annotation libraries like

import android.support.annotation.DrawableRes;

with the AndroidX ones:

import androidx.annotation.DrawableRes;

Some libraries used in the build.gradle file were also updated. Therefore, after these modifications, I had to include this as a new module in the project since its now a customized library.

If I get some time then I will try to fork this project, update the libraries and generate the PR but I am quite occupied by work these days. It would be great if you or someone else could do the needful.