How to count the number of letters that you see in TextView?

All. I am a new in android developing. Now I am working with TextView. As the case may be I set differnt text. But sometime text is very big. For example, I insert this "abcdefgh_myword", and i get something like that "abcdefg" in my small TextView, but i want to get this "..._myword". So i want to count the number of letters that i see (programly) and set text what i want to see. How to do this? Any ideas?

....

android:ellipsize="start" doesn't work how i want... Please watch what happen when i constrict width my TextView

before

enter image description here

after

enter image description here

<TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:ellipsize="start"
                android:gravity="center"
                android:maxLines="1"
                android:text="abcdef1234567" />

Any ideas?

Upvotes: 0

Views: 1302

Answers (4)

Hani
Hani

Reputation: 104

This could help you

android:ellipsize="start"
android:maxLines="1"

add it to the TextView in your XML file

EDIT:

According to this: https://stackoverflow.com/a/39890995/7325737 you have to use android:singleLine="true" because only end and marquee are supported with maxLines

Upvotes: 1

lidkxx
lidkxx

Reputation: 2831

You're probably looking for Java's string length() method. Check this answer. Then either use android:ellipsize="start" in XML or setEllipsize(TruncateAt.START); as needed.

Upvotes: 0

drawinfinity
drawinfinity

Reputation: 315

In addition to lidkxx 's answer you would call getText() on your text view to get the string.

TextView textview = (TextView) findViewById(R.id.your_text_view);
String s = textview.getText();
int letterCount = s.length();

Upvotes: 0

F43nd1r
F43nd1r

Reputation: 7749

This would be only possible if you knew how wide each symbol is. Which you normally don't, as the font is system dependent.

You could use a monospaced font for this.

Upvotes: 0

Related Questions