Android-ExpandableTextView
Android-ExpandableTextView copied to clipboard
How determine if need expand the text?
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 .
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!
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);
}
}
});
}
thanks for the tip! I'll take a look at it and try to integrate it in ExpandableTextView
just letting you know that I'm finally started working on this feature. I'll keep you posted!
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);
}
}
});
}
}
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);
}
}
});
If someone is still looking for this feature Please see this answer
Hi @Cliffus Any solution to this issue? Have you incorporated some method in your library ?
@manish-poddar Use this https://github.com/vkdinventor/Android-ExpandableTextView
@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
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");
}
}
}
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...