Set Text View ellipsize and add view more at end
Find my answer
public static void makeTextViewResizable(final TextView tv, final int maxLine, final String expandText, final boolean viewMore) {
if (tv.getTag() == null) {
tv.setTag(tv.getText());
}
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = tv.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if (maxLine == 0) {
int lineEndIndex = tv.getLayout().getLineEnd(0);
String text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
tv.setText(text);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(
addClickablePartTextViewResizable(Html.fromHtml(tv.getText().toString()), tv, maxLine, expandText,
viewMore), TextView.BufferType.SPANNABLE);
} else if (maxLine > 0 && tv.getLineCount() >= maxLine) {
int lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);
String text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
tv.setText(text);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(
addClickablePartTextViewResizable(Html.fromHtml(tv.getText().toString()), tv, maxLine, expandText,
viewMore), TextView.BufferType.SPANNABLE);
} else {
int lineEndIndex = tv.getLayout().getLineEnd(tv.getLayout().getLineCount() - 1);
String text = tv.getText().subSequence(0, lineEndIndex) + " " + expandText;
tv.setText(text);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(
addClickablePartTextViewResizable(Html.fromHtml(tv.getText().toString()), tv, lineEndIndex, expandText,
viewMore), TextView.BufferType.SPANNABLE);
}
}
});
}
private static SpannableStringBuilder addClickablePartTextViewResizable(final Spanned strSpanned, final TextView tv,
final int maxLine, final String spanableText, final boolean viewMore) {
String str = strSpanned.toString();
SpannableStringBuilder ssb = new SpannableStringBuilder(strSpanned);
if (str.contains(spanableText)) {
ssb.setSpan(new MySpannable(false){
@Override
public void onClick(View widget) {
if (viewMore) {
tv.setLayoutParams(tv.getLayoutParams());
tv.setText(tv.getTag().toString(), TextView.BufferType.SPANNABLE);
tv.invalidate();
makeTextViewResizable(tv, -1, "See Less", false);
} else {
tv.setLayoutParams(tv.getLayoutParams());
tv.setText(tv.getTag().toString(), TextView.BufferType.SPANNABLE);
tv.invalidate();
makeTextViewResizable(tv, 3, ".. See More", true);
}
}
}, str.indexOf(spanableText), str.indexOf(spanableText) + spanableText.length(), 0);
}
return ssb;
}
Another class:-
import android.graphics.Color;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
public class MySpannable extends ClickableSpan {
private boolean isUnderline = true;
/**
* Constructor
*/
public MySpannable(boolean isUnderline) {
this.isUnderline = isUnderline;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(isUnderline);
ds.setColor(Color.parseColor("#1b76d3"));
}
@Override
public void onClick(View widget) {
}
}
Last step to call it:
DetailTv.setText(discription);
makeTextViewResizable(DetailTv, 3, "See More", true);
Android Expandable text view with View More Button displaying at center after 3 lines
This is how i have achieved the desired output,
MytextView which i want to expand is: tvDescription
I have a see more button with name: btnSeeMore
To check if the textview has more than 4 lines i had a listener for it as follows,
tvDescription.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if(expandable) {
expandable = false;
if (tvDescription.getLineCount() > 4) {
btnSeeMore.setVisibility(View.VISIBLE);
ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 4);
animation.setDuration(0).start();
}
}
}
});
I have kept a boolean value to check if textview is already expanded so that there will not be any hickups while collapsing it.
if textview has more than four lines, boolean flag will be true and the button will be visible so this is the code for expanding and collapsing animation.
btnSeeMore.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!expand) {
expand = true;
ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 40);
animation.setDuration(100).start();
btnSeeMore.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_collapse));
} else {
expand = false;
ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 4);
animation.setDuration(100).start();
btnSeeMore.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_expand));
}
}
});
Comment below for further information
Android, How to limit width of TextView (and add three dots at the end of text)?
Deprecated:
Add one more property android:singleLine="true"
in your Textview
Updated:
android:ellipsize="end"
android:maxLines="1"
Ellipsize not show three dots at end of TextView
I used ellipsize=true
in the project I developed before, and it worked well in that project, I compared the code of the current project with the previous one. There is no difference between each other, so I am confused with the current project, so I pulled the previous project from github and ran it, and found that it NOT WORK in my xml renderer, but it WORKS on my phone.
Eventually I found it is the problem of the renderer. I found the button to upgrade the renderer. Now I use the latest renderer, and it also works fine in the redener now.
How to show three dots at the end of text view
You're using ConstraintLayout
inside CardView
and I can see that you've constrained the TextView
properly but its width is wrap_content
which what causing the issue. set it to 0dp
and your android:ellipsize
and android:singleLine
attributes will work.
Try this:
<TextView
...
android:layout_width="0dp"
... />
How to show 3 dots at the end of text in textview
Make a preset width for your TextView and add these attributes (Shout-out to @T-D)
android:ellipsize="end"
android:maxLines="1"
The maxLines will make sure that your text wont be shown in two lines and the ellipsize will add the three dots in the end.
Old Answer
android:ellipsize="end"
android:singleLine="true"
Display three dots at the end of the textview
Add a drawablePadding
attribute along with maxLines
and ellipsize
.
Try this,
<EditText
android:id="@+id/etZipCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLength="8"
android:text="3445"
android:padding="15dp"
android:singleLine="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:drawableRight="@drawable/ic_dropdown_pin"
android:minHeight="50dp"
android:padding="15dp"
android:text="Alaska Standard Time"
android:ellipsize="end"
android:maxLines="1"
android:drawablePadding="10dp"
android:singleLine="true"/>
</LinearLayout>
Screenshot below,
Related Topics
How to Set Multiple Alarms Using Alarm Manager in Android
Failed to Resolve Com.Android.Support:Appcompat-V7:22 and Com.Android.Support:Recyclerview-V7:21.1.2
Removing Unused Strings During Proguard Optimisation
Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget
How to Use Setarguments() and Getarguments() Methods in Fragments
How to Open an Android Studio Project in Eclipse
Custom Row in a Listpreference
Sliding Drawer Appear in All Activities
Error Strictmode$Androidblockguardpolicy.Onnetwork
Delete Alarm from Alarmmanager Using Cancel() - Android
How to Force the Action Bar to Be at the Bottom in Ics
Android: Wrap_Content Is Not Working with Listview
Android Setonclicklistener Method - How Does It Work
Visual Studio (2015) Emulator for Android Not Working - Xde.Exe - Exit Code 3
Check If Battery Optimization Is Enabled or Not for an App
Resizing Layouts Programmatically (As Animation)
How to Get Access Token After User Is Signed in from Gmail in Android