Search code examples
androidandroid-listviewtextviewellipsis

Text of TextView in ListView not being ellipsized


I am trying to add ellipses to a textview in my listview and it's not working.

Please see my xml definition below.

In this case, it's not coming either on the emulator or the device:

?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:focusable="true">
    <ImageView android:id="@+id/imageview" android:layout_width="30dip"
        android:layout_height="30dip" android:layout_centerVertical="true"
        android:layout_alignParentLeft="true" android:src="@drawable/about"
        android:layout_margin="8dip"/>
    <TextView android:id="@+id/title" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageview"
        android:textStyle="bold" android:textColor="#000000"
            android:textSize="15sp" android:lines="1" 
        android:ellipsize="marquee" android:inputType="text"
        android:marqueeRepeatLimit="3" 
        android:layout_marginRight="15dip"
        android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/delete">
    </TextView>
    <ImageButton android:id="@+id/delete" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_centerVertical="true"
        android:layout_alignParentRight="true" android:layout_marginRight="0dip"
        android:layout_margin="5dip" android:src="@drawable/delete_icon"
        />


</RelativeLayout>

And in this case, it's coming on the emulator, not on the device:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView android:id="@+id/imageview"
    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
                         android:layout_centerVertical="true" android:layout_alignParentLeft="true"/>
        <TextView android:id="@+id/title" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageview"
             android:textStyle="bold" android:textColor="#000000"
             android:layout_marginLeft="10dip" android:layout_marginRight="10dip"
            android:textSize="15sp"  android:cacheColorHint="#00000000"> 
        </TextView>
        <TextView android:id="@+id/shortdesc" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_below="@+id/title"
            android:layout_toRightOf="@+id/imageview" 
             android:textSize="15sp"
            android:lines="2" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever"
             android:layout_marginLeft="10dip" android:layout_marginRight="20dip"
            android:textColor="#413839"  android:cacheColorHint="#00000000">

        </TextView>


</RelativeLayout>

Please help, thanks for your input and time


The solutions For the first one pls see my commnets below

For the second one, i had to replace marquee with end

<TextView android:id="@+id/shortdesc" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_below="@+id/title"
        android:layout_toRightOf="@+id/imageview" 
         android:textSize="15sp" 
        android:maxLines="2"  android:ellipsize="end" 
         android:layout_marginLeft="10dip" android:layout_marginRight="30dip"
        android:textColor="#413839"  android:cacheColorHint="#00000000"
        android:text="asdfghjmsbvhsfadk vcdfkhreouhjkjfdgdslkfdn bfoiunbf m,cvbkjnfgb kj      nbcdjfkdleoig;dkf nmvnb safdsfrtgdfgdfhg">
    </TextView>

Solution

  • if its not working in listview try to first display ellipsized textview as a standalone view listview some time can complicate things. and also try to use attribute "singleLine=false"

    also have a look at this question: lots of info related to ellipse sizing the textview.

    updated: my textview working perfectly in my app

    <TextView 
        android:id="@+id/product_desc" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/img_disclosure"
            android:layout_marginTop="5dip"
            android:layout_marginRight="5dip"
            android:ellipsize="marquee" 
            android:text="this is the sample text to test the ellipsize effect of text view"
            android:textColor="@color/results_sep_color" 
            android:textSize="15sp"
            android:textStyle="bold"
            android:singleLine="false"
            android:maxLines="2"/>
    

    Second update:

    <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="Don't have an account sdsadfsf asdfsdfdsdd abracadabra"
    
          />