Skip to content

How determine if need expand the text? #2

Open

Description

@webserveis

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 .

Activity

  1. Cliffus commented on May 24, 2016

    @Cliffus
    Contributor

    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!

  2. added this to the 1.1.0 milestone on May 28, 2016
  3. self-assigned this
    on May 28, 2016
  4. webserveis commented on Jun 5, 2016

    @webserveis
    Author

    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);
                        }
                    }
                });
            }
    
  5. Cliffus commented on Jun 5, 2016

    @Cliffus
    Contributor

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

  6. Cliffus commented on Aug 2, 2016

    @Cliffus
    Contributor

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

  7. RedshirtMB commented on Sep 27, 2016

    @RedshirtMB

    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);
                    }
                }
            });
        }
    }
    
  8. Julia-Ts commented on Mar 29, 2017

    @Julia-Ts

    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);
                    }
                }
            });
    
  9. vkdinventor commented on Jun 24, 2017

    @vkdinventor

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

  10. added a commit that references this issue on Jun 24, 2017
  11. manish-poddar commented on May 15, 2018

    @manish-poddar

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

  12. manish-poddar commented on Jun 21, 2018

    @manish-poddar

    @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.

  13. vkdinventor commented on Jun 27, 2018

    @vkdinventor

    @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

  14. J6ey commented on Dec 15, 2018

    @J6ey

    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");
            } 
        } 
    }
    
  15. sagarpatel288 commented on Feb 18, 2019

    @sagarpatel288

    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...

  16. added a commit that references this issue on Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions