Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 'ellipsize' in a TextView and stretch the view ONLY if necessary

I have 2 TextViews in a row and 2 requirements:

1) If the first TextView is not too wide, it should look as follows

|[1 text][2 text]               |

2) If the first TextView is too wide, it should look as follows

|[1 text text tex...][2 text]|

The second requirement is simple, you can use android:layout_weight="1", e.g.:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
        android:text="1 text text text text text"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2 text"
    />
</LinearLayout>

, but if the first TextView contains a short text, it looks like

|[1 text               ][2 text]|

, which is not acceptable.

So how to satisfy both 1) and 2) requirements at the same time?

like image 914
zmiter.freeman Avatar asked Jan 14 '23 09:01

zmiter.freeman


1 Answers

In the meantime I found a very simple solution: just set the LinearLayout width to "wrap_content" instead of "fill_parent".

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="WRAP_CONTENT"
    android:layout_height="wrap_content"
>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
        android:text="1 text text text text text"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2 text"
    />
</LinearLayout>
like image 114
zmiter.freeman Avatar answered Mar 16 '23 00:03

zmiter.freeman