Search code examples
androidxmlresizetextviewautoresize

How can I display two horizontal textviews, one of which resizes depending on content?


I have a ListView that shows two TextViews, like so:

| Each row   |    has the |
| same width | as the row |
| before it  |  like this |

But I want to do something like this:

| Each left column | uses |
| as much space |   as it |
| needs |       like this |

Can this be done with just XML? I have tried modifying this solution, but it did not work correctly.


Solution

  • You should wrap text views in RelativeLayout. Specify android:layout_width="wrap_content" for both, and android:layout_alignParentLeft="true" and android:layout_alignParentRight="true" for the left and right TextViews respectively. Set android:gravity="left" and android:gravity="right" respectively, as well

    But in this case long text may overlap

    another option is to use layout_weight:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="left"
            android:text="some text"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="right"
            android:text="text"/>
    
    </LinearLayout>