Android-ExpandableTextView icon indicating copy to clipboard operation
Android-ExpandableTextView copied to clipboard

How determine if need expand the text?

Open webserveis opened this issue 9 years ago • 13 comments

How can you get if you need to expand the text? ie when the text takes up very little , so as not to show the expand button .

webserveis avatar May 23 '16 15:05 webserveis

Hi

the ExpandableTextView currently doesn't have support for the described behaviour. I'll look into it and get back to you soon.

Thanks for using ExpandableTextView and reporting this feature request!

Cliffus avatar May 24 '16 10:05 Cliffus

Hi,

To determine if you have to expand or not , I use that code , and I was a copy paste of a set of tips, if not the best way, I am a newbie in Android

        final View myView = findViewById(R.id.expandableTextView);
        if (myView != null) {
            myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @SuppressWarnings("deprecation")
                @Override
                public void onGlobalLayout() {

                    Log.d(TAG, "getLineCount " + (expandableTextView != null ? expandableTextView.getLineCount() : 0));
                    Log.d(TAG, "getMaxLines " + (expandableTextView != null ? expandableTextView.getMaxLines() : 0));

                    if (expandableTextView != null) {

                        int start = expandableTextView.getLayout().getLineStart(0);
                        int end = expandableTextView.getLayout().getLineEnd(expandableTextView.getLineCount() - 1);

                        String displayed = expandableTextView.getText().toString().substring(start, end);

                        if (displayed.length() == expandableTextView.getText().length()) {
                            Log.d(TAG, "NO NEED TO EXPAND ");
                        } else {
                            Log.d(TAG, "NEED TO EXPAND ");
                        }

                    }

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        myView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        myView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
                }
            });
        }

webserveis avatar Jun 05 '16 12:06 webserveis

thanks for the tip! I'll take a look at it and try to integrate it in ExpandableTextView

Cliffus avatar Jun 05 '16 15:06 Cliffus

just letting you know that I'm finally started working on this feature. I'll keep you posted!

Cliffus avatar Aug 02 '16 11:08 Cliffus

I liked the suggestion by @webserveis . I wrapped the widget in a widget of my own, cleverly called ExpandableTextView :) I set the global listener right before setting the text.

public class ExpandableTextView extends LinearLayout {

    private at.blogc.android.views.ExpandableTextView textView;
    private Button button;
    private LinearLayout expander;

    public ExpandableTextView(Context context) {
        super(context);
        initialize(context);
    }

    public ExpandableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    public ExpandableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    @TargetApi(21)
    public ExpandableTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initialize(context);
    }

    private void initialize(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_expandable_textview, (ViewGroup) getRootView(), true);

        textView = (at.blogc.android.views.ExpandableTextView) findViewById(R.id.expanderText);
        textView.setAnimationDuration(1000L);
        textView.setInterpolator(new OvershootInterpolator());
        textView.setOnExpandListener(new at.blogc.android.views.ExpandableTextView.OnExpandListener() {
            @Override
            public void onExpand(at.blogc.android.views.ExpandableTextView view) {
                button.setText(R.string.less);
            }

            @Override
            public void onCollapse(at.blogc.android.views.ExpandableTextView view) {
                button.setText(R.string.more);
            }
        });
        button = (Button) findViewById(R.id.expanderMoreButton);
        expander = (LinearLayout) findViewById(R.id.expander);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.toggle();
            }
        });
    }

    public void setText(CharSequence text) {
        addGlobalListener();
        textView.setText(text);
    }

    private void addGlobalListener() {
        if (textView == null) return;
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                int start = textView.getLayout().getLineStart(0);
                int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);

                String displayed = textView.getText().toString().substring(start, end);

                if (displayed.length() == textView.getText().length()) {
                    expander.setVisibility(GONE);
                } else {
                    expander.setVisibility(VISIBLE);
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            }
        });
    }
}

RedshirtMB avatar Sep 27 '16 05:09 RedshirtMB

Thanks for the solution provided above, but it works only if text contains "\n". It is better to use ellipsize attribute on ExpandableTextView and check for it in OnGlobalLayoutListener like this:

 getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

     @Override
            public void onGlobalLayout() {
                Layout layout = textView.getLayout();
                if (layout != null) {
                    int lines = layout.getLineCount();
                    if (lines > 0) {
                        int ellipsisCount = layout.getEllipsisCount(lines - 1);
                        if (ellipsisCount > 0) {
                            expander.setVisibility(VISIBLE);
                        } else {
                            expander.setVisibility(GONE);
                        }
                    }
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            }
        });

Julia-Ts avatar Mar 29 '17 17:03 Julia-Ts

If someone is still looking for this feature Please see this answer

vkdinventor avatar Jun 24 '17 14:06 vkdinventor

Hi @Cliffus Any solution to this issue? Have you incorporated some method in your library ?

manish-poddar avatar May 15 '18 11:05 manish-poddar

@manish-poddar Use this https://github.com/vkdinventor/Android-ExpandableTextView

vkdinventor avatar Jun 20 '18 20:06 vkdinventor

@vkdinventor isn't this the same library you forked? I couldn't see any method which determines the need to expand. If there is some, please give me the snippet.

manish-poddar avatar Jun 21 '18 03:06 manish-poddar

@manish-poddar In my version of this library, you can set maximum line count. So if text takes less than max line count, expand button would be disabled. see commit

vkdinventor avatar Jun 27 '18 19:06 vkdinventor

An even easier way is to just check if the ExpandableTextView is ellipsized.

Layout layout = expTextView.getLayout();
if(layout != null) {
    int lines = layout.getLineCount();
    if(lines > 0) {
        int ellipsisCount = layout.getEllipsisCount(lines-1);
        if ( ellipsisCount > 0) {
            Log.d(TAG, "Text is ellipsized; show expand button");
        } 
    } 
}

J6ey avatar Dec 15 '18 04:12 J6ey

If anyone has problem getting this done properly in recyclerView, simply set value first in onBind and then in handler, check line count and execute your logic based on line count...

sagarpatel288 avatar Feb 18 '19 09:02 sagarpatel288