Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView android:ellipsize="end" issue

In my application, I've ellipsized 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.

like image 430
Rajkiran Avatar asked Mar 26 '12 11:03

Rajkiran


People also ask

What is android Ellipsize end?

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.

How do you get 3 dots at the end of a TextView text?

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!

How do you center text in TextView?

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.

How do I add a newline to a TextView in android?

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.


2 Answers

//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.

like image 111
Padma Kumar Avatar answered Nov 11 '22 04:11

Padma Kumar


 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);

                }

            }
        });
like image 26
Syamantak Basu Avatar answered Nov 11 '22 04:11

Syamantak Basu