I'm laying out 3 views horizontally on the screen (fixed-size image and 2 single-line text views: leftTextView
and rightTextView
) and I'm trying to get the rightTextView
to hug against the leftTextView
, but in the event that the width of both labels would exceed the screen size, truncate the leftTextiew
.
Example of desired functionality:
|img|leftText|rightText| ||(end of screen)
|img|leftTextMedium|rightText| ||
|img|leftTextTooLongSoTrunc...|rightText||
What's actually happening:
|img|leftText|rightText| ||(end of screen)
|img|leftTextPrettyLongButNotHuge|rightT||
|img|leftTextWhichIsIncrediblyLongBlahBl||
Currently, if the size of both text views exceeds the view's width, the rightTextView
ends up getting squished down to make room, even though I've set android:ellipsize="end"
on the leftTextView
.
Is there a way to make the leftTextView
have a "truncation priority" that says it should truncate if it would cause other views to not fit? Or on the other side, can I set the rightTextView
to have a "compression resistance" in order to keep it from being squished to make room for the leftTextView
? Here's a sample of my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/fixedWidthImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/leftTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/fixedWidthImage"
android:layout_toRightOf="@id/fixedWidthImage"
android:maxLines="1"
android:textSize="18sp"
android:ellipsize="end" />
<TextView
android:id="@+id/rightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/leftTextView"
android:layout_toRightOf="@+id/leftTextView"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:maxLines="1"
android:textSize="12sp" />
</RelativeLayout>
Things I've tried:
- In the
leftTextView
, add atoStartOf
/toLeftOf
therightTextView
(crashes the app) Instead of aligning the
rightTextView
to the end/right of theleftTextView
, flip it and align theleftTextView
to the start/left of therightTextView
. This results in therightTextView
being anchored to the end of the screen instead of directly to the right of theleftTextView
. Example of how this ends up looking:|img|leftText |rightText|| |img|leftTextMedium |rightText|| |img|leftTextTooLongSoTrunc...|rightText||
Even if I set
android:minWidth="25dp"
just to ensure at least part of therightTextView
is visible (which I don't want to do because this text length is variable) it still ends up compressing the width to make room for theleftTextView
.- Per Emanuel Moecklin's answer, I tried wrapping in a
LinearLayout
and setting the weight on theleftTextView
. The result was visually identical to what I tried in #2.