You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 .
Reacted by Dawid Hyży, Denis Marinx, Adrian Coman, Taras Shevchenko, Maksim Lin, davit-zakaryan, Anisur Rahman Tonu, Hakob Vardanyan and Petar Kramaric
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);
}
}
});
}
}
Reacted by Webserveis, Anh Duong, Julia-Ts, Luka Dulčić and luisg122
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);
}
}
});
Reacted by Luka Dulčić, Denis Marinx, GreenVegetables, Tuan Giao , Maksim Lin and luisg122
@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 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
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...
Activity
Cliffus commentedon 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!
webserveis commentedon Jun 5, 2016
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
Cliffus commentedon Jun 5, 2016
thanks for the tip! I'll take a look at it and try to integrate it in ExpandableTextView
Cliffus commentedon Aug 2, 2016
just letting you know that I'm finally started working on this feature. I'll keep you posted!
RedshirtMB commentedon Sep 27, 2016
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.
Julia-Ts commentedon 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:
vkdinventor commentedon Jun 24, 2017
If someone is still looking for this feature Please see this answer
fix for issue Blogcat#2 when text takes up less line than threshold, …
manish-poddar commentedon May 15, 2018
Hi @Cliffus
Any solution to this issue? Have you incorporated some method in your library ?
vkdinventor commentedon Jun 20, 2018
@manish-poddar Use this https://github.com/vkdinventor/Android-ExpandableTextView
manish-poddar commentedon Jun 21, 2018
@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 commentedon 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 commentedon Dec 15, 2018
An even easier way is to just check if the ExpandableTextView is ellipsized.
sagarpatel288 commentedon Feb 18, 2019
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...
Notify on expanded Blogcat#2