Skip to main content

Android: TextView automatically truncate and replace last 3 char of String


If a String is longer than the TextView's width it automatically wraps onto the next line. I can avoid this by using android:singleLine (deprecated) or by setting android:inputType="text" . What I now need is something that replaces the last 3 characters of my String with " ... ". Since I'm not using a monospace font this will always be different depending on the letters used in my String. So I'm wondering what's the best way to get the last 3 characters of a String in a TextView and replace them. Maybe there's already something implemented in the Android framework, since this must me a common problem.



Source: Tips4allCCNA FINAL EXAM

Comments

  1. You should be able to use the "ellipsize" property of a text view:

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_mytext"
    android:ellipsize="end"
    android:singleLine="true"
    />


    You may also need to apply gravity values to the layout too; I have sometimes seen "auto-stretching" views without them.

    ReplyDelete
  2. Found an interesting work-a-round for this problem.

    maxLines=1
    ellipsize=end
    scrollHorizontally=true


    The trick is that last statement about horizontal scrolling .... check it out. It at least works on v2.2.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

Java Urban Myths

Along the line of C++ Urban Myths and Perl Myths : What are the Java Urban Myths? That is, the ideas and conceptions about Java that are common but have no actual roots in reality . As a Java programmer, what ideas held by your fellow Java programmers have you had to disprove so often that you've come to believe they all learned at the feet of the same drunk old story-teller? Ideally, you would express these myths in a single sentence, and include an explanation of why they are false.