Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How determine if need expand the text? #2

Open
webserveis opened this issue May 23, 2016 · 13 comments
Open

How determine if need expand the text? #2

webserveis opened this issue May 23, 2016 · 13 comments
Assignees
Milestone

Comments

@webserveis
Copy link

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 .

@Cliffus
Copy link
Contributor

Cliffus commented May 24, 2016

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 Cliffus added this to the 1.1.0 milestone May 28, 2016
@Cliffus Cliffus self-assigned this May 28, 2016
@webserveis
Copy link
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);
                    }
                }
            });
        }

@Cliffus
Copy link
Contributor

Cliffus commented Jun 5, 2016

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

@Cliffus
Copy link
Contributor

Cliffus commented Aug 2, 2016

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

@RedshirtMB
Copy link

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);
                }
            }
        });
    }
}

@Julia-Ts
Copy link

Julia-Ts commented Mar 29, 2017

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);
                }
            }
        });

@vkdinventor
Copy link

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

vkdinventor added a commit to vkdinventor/Android-ExpandableTextView that referenced this issue Jun 24, 2017
@manish-poddar
Copy link

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

@vkdinventor
Copy link

@manish-poddar
Copy link

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

@vkdinventor
Copy link

vkdinventor commented Jun 27, 2018

@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

@J6ey
Copy link

J6ey commented Dec 15, 2018

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");
        } 
    } 
}

@sagarpatel288
Copy link

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

igor-alyoshin pushed a commit to igor-alyoshin/Android-ExpandableTextView that referenced this issue Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants