Bins Ich Bins Ich - 1 year ago 124
Android Question

TextView Ellipsize (...) not working

I want to have a single lined TextView to show up 3 dots at the end when the text is longer than the TextView. I don't know why - but i don't get it.

I already wrapped my head around similar stackoverflow questions - but i ended up with no solution. Maybe someone has some useful hints.

<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent"
<TextView android:textStyle="bold" android:text="Full Name" android:layout_height="wrap_content" android:textSize="16sp"
android:layout_width="wrap_content" android:id="@+id/lName"
android:layout_gravity="center_vertical" android:maxLines="1" android:ellipsize="end"/>

The linear Layout above is nested into 2 other Linear Layouts. Maybe this is important to know. I already tried the attribute "singleLine" too, but some say this is deprecated and it doesnt work anyway.

Answer Source

Add the following styles in your styles file (typically styles.xml):

<style name="autoscroll">
    <item name="android:singleLine">true</item>
    <item name="android:ellipsize">marquee</item>
    <item name="android:marqueeRepeatLimit">marquee_forever</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:scrollHorizontally">true</item>

Then add the style @style/autoscroll to your TextView:

<TextView android:id="@+id/lName"
      style="@style/autoscroll" />

You can reuse your autoscroll feature easily when you want this way.