Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed width TextView next to variable width TextView

I'm trying to create a layout with two TextViews next to each other. The first TextView has a variable length and is single-lined. The second TextView contains a fixed label and should not be ellipsized. The second label has to be right next to the first one. Here are two examples of what I'm trying to accomplish:

enter image description here enter image description here

This is what I've achieved so far:

enter image description here

With this layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/my_bg"
            android:padding="16dp"
tools:layout_height="60dp">

<TextView
    android:layout_centerVertical="true"
    android:id="@+id/tv_street"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/tv_updated"
    android:textSize="17sp"
    android:singleLine="true"
    android:ellipsize="end"
    android:layout_marginLeft="16dp"
    tools:text="This label is short"/>

<TextView
    android:id="@id/tv_updated"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:text="@string/updated"
    android:textAllCaps="true"
    android:textColor="@color/red"
    android:textStyle="bold"/>
</RelativeLayout>

The remaining problem is that the container gets a width that matches its parent while it should be wrap_content.

Are there any other ways to fix this?

like image 435
dumazy Avatar asked Feb 19 '26 00:02

dumazy


1 Answers

you better use linearlayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:padding="16dp"
              android:orientation="horizontal"
              android:gravity="center_vertical"
              tools:layout_height="60dp">
    <TextView
        android:id="@+id/tv_street"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_marginLeft="16dp"
        tools:text="This label is short"/>

    <TextView
        android:id="@id/tv_updated"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="UPDATED"
        android:textAllCaps="true"
        android:textColor="@color/red"
        android:textStyle="bold"/>
</LinearLayout>

edited: here the prove

tested

like image 79
aiwiguna Avatar answered Feb 21 '26 13:02

aiwiguna