How can I show ellipses on my TextView if it is greater than the 1 line?
This is a common problem. Try using the following:
android:scrollHorizontally="true"
android:ellipsize="end"
android:maxLines="1"
.............. the scrollHorizontally is the "special sauce" that makes it work.
How to use custom ellipsis in Android TextView
I've finally managed it in this way (may be not the best one):
private void setLabelAfterEllipsis(TextView textView, int labelId, int maxLines){
if(textView.getLayout().getEllipsisCount(maxLines-1)==0) {
return; // Nothing to do
}
int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
String displayed = textView.getText().toString().substring(start, end);
int displayedWidth = getTextWidth(displayed, textView.getTextSize());
String strLabel = textView.getContext().getResources().getString(labelId);
String ellipsis = "...";
String suffix = ellipsis + strLabel;
int textWidth;
String newText = displayed;
textWidth = getTextWidth(newText + suffix, textView.getTextSize());
while(textWidth>displayedWidth){
newText = newText.substring(0, newText.length()-1).trim();
textWidth = getTextWidth(newText + suffix, textView.getTextSize());
}
textView.setText(newText + suffix);
}
private int getTextWidth(String text, float textSize){
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextSize(textSize);
paint.getTextBounds(text, 0, text.length(), bounds);
int width = (int) Math.ceil( bounds.width());
return width;
}
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.
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);
How to show long text only show 1 lines and append with
Check this out ... Add the scrollHorizontally element also.
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"
How do I tell if my textview has been ellipsized?
You can get the layout of the TextView
and check the ellipsis count per line. For an end ellipsis, it is sufficient to check the last line, like this:
Layout l = textview.getLayout();
if (l != null) {
int lines = l.getLineCount();
if (lines > 0)
if (l.getEllipsisCount(lines-1) > 0)
Log.d(TAG, "Text is ellipsized");
}
This only works after the layout phase, otherwise the returned layout will be null, so call this at an appropriate place in your code.
How to show ellipsis (three dots) at the end of a Text line in Android Jetpack Compose?
Both BasicText and Text have overflow
and maxLines
arguments which can help you.
Text(myText, maxLines = 1, overflow = TextOverflow.Ellipsis)
Here's a full single-line example:
import androidx.compose.material.Text
import androidx.compose.ui.text.style.TextOverflow
@Composable
fun EllipsisExample() {
Box(modifier = Modifier.width(160.dp)) {
Text(
text = "Lorem ipsum dolor sit amet.",
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}
Of course you can tune maxLines
to fit your needs:
Text(
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
maxLines = 2,
overflow = TextOverflow.Ellipsis
)
Related Topics
How to Change Size of Title's Text on Action Bar
Is Dalvik's Memory Model the Same as Java'S
Why Menuitemcompat.Getactionprovider Returns Null
What Is the Equivalent to a JavaScript Setinterval/Settimeout in Android/Java
How Get Value from Linkedhashmap Based on Index Not on Key
Binary Xml File Line #0: Error Inflating Class Imageview
Android:Table Has No Column Named "Variable Name" MySQL Database Error
Fixing 'Path' in Environment Variables in Windows 7 for Multiple Applications
Choosing Java VS Python on Google App Engine
Converting Utf-8 to Iso-8859-1 in Java - How to Keep It as Single Byte
:App:Dexdebug Execexception Finished with Non-Zero Exit Value 2
How to Change the Edittext Text Without Triggering the Text Watcher
Android Proguard, Keep Inner Class
Transparent Blurry View Which Blurs Layout Underneath
How to Remove Only Trailing Spaces of a String in Java and Keep Leading Spaces
Broadcastreceiver for Action_Media_Button Not Working
How to Install "Android Support Library" to Deploy a Gluon Mobile Application to Android