I've got two TextViews side-by-side. TextView1 has a varying length of text, and TextView2 always says "+#". When TextView1 gets long however, it pushes TextView2 off screen. Any ideas how to fix this? Here's my layout code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:textSize="13sp"/>
<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="13sp"/>
</RelativeLayout>
This is actually something I've tried to solve for a while now. Unfortunately, the method others have suggested - using layout_weight
inside LinearLayout
- doesn't actually work; however, I've found a solution for you!
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left">
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/TextView2"
android:singleLine="true"
android:ellipsize="end"
android:textSize="13sp"/>
<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:singleLine="true"
android:textSize="13sp"/>
</RelativeLayout>
With the above block, we use a RelativeLayout
in order to align the first TextView
to the left of the second TextView
. We also align the second TextView
to the right side of the parent ViewGroup
. Finally, we add android:gravity="left"
to the parent ViewGroup
in order to align all of the TextView
's to the left.
This results in both TextView
's being side by side - regardless of the first TextView
's length. If you would like the first TextView
to have multiple lines, simply remove the android:ellipsize="end"
tag.
Hopefully this is your expected outcome!
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