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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With