In my application, I've ellipsize
d my TextView
so that if the text is too large, it'll show ...
at the end using android:ellipsize="end"
.
e.g.Text ABCDEFGHIJKLMNOPQRSTUVWXYZ
is shown as ABCDEFGHIJ...
. But when the text is small, like ABCDEF
it still shows ...
at the end making the text look like AB...
. I can not fix the width of the textview since the same textview is used elsewhere with some different width requirements. What should I do to make it work and show ...
only if the text it large enough.
What is Ellipsize in android TextView? Android Ellipsize Android TextView ellipsize property Causes words in the text that are longer than the view's width to be ellipsized ( means to shorten text using an ellipsis, i.e. three dots …) instead of broken in the middle to fit it inside the given view.
You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!
android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered.
Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.
//In your textview
add the below attributes also
android:maxEms="8"
android:singleLine="true"
NOTE: you can adjust the ems size to how many chars you want to show.
txtvw.setText("The Indian economy is the world's eleventh-largest by nominal GDP and third-largest by purchasing power parity (PPP). " +
" Following market-based economic reforms in 1991, India became one of the fastest-growing major economies; " +
" it is considered a newly industrialised country. However, it continues to face the challenges of poverty, illiteracy, corruption, malnutrition, and inadequate public healthcare. " +
" A nuclear weapons state and a regional power, it has the third-largest standing army in the world and ranks ninth in military expenditure among nations." +
" India is a federal constitutional republic governed under a parliamentary system consisting of 28 states and 7 union territories. " +
" India is a pluralistic, multilingual, and multiethnic society. " +
" It is also home to a diversity of wildlife in a variety of protected habitats.");
ViewTreeObserver vto = txtvw.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = txtvw.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if(txtvw.getLineCount() > 1){
int lineEndIndex = txtvw.getLayout().getLineEnd(1);
String text = txtvw.getText().subSequence(0, lineEndIndex-3)+"...";
txtvw.setText(text);
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With