Search code examples
androidtextword-wrap

How to wrap text based on remaining space and add three dots?


how should I work with my textview to wrap the text when it almost reach the half of the screen, I've done it using text.substring and limit the text length to 15 and add three dots but I tested it on my s4, it don't even reach the half, 15 text length is too small to wrap it, is there's a way to substring it using the remaining space?

To clarfy the question, I have a textview and when the text length reach the half of the screen width it cuts the text and add three dots at the end.


Solution

  • Here is one way of doing it.

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Use a linear layout to fill the width -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <!-- using weights of 1, will divide screen into two halves -->
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:padding="5dp"
            android:text="This is a test for ellipsize. You can see it works :)"
            android:maxLines="1" />
    
        <!-- this is basically a dummy view, nothing will be displayed -->
    
        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1" />
    
    </LinearLayout>
    

    I haven't tried it in IDE, but I think it should work. Hope this helps.