0

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.

Community
  • 1
  • 1
BLuFeNiX
  • 2,496
  • 2
  • 20
  • 40

2 Answers2

1

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>
Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
  • I tried this, and the text on the left will overlap with the text on the right. Is there a way to make sure there is padding between them? – BLuFeNiX Sep 11 '14 at 18:47
  • yes, for padding try `LinearLayout` solution (see edited) and add `androd:paddingRight="3dp"` to the left TextView, or `androd:paddingLeft="3dp"` to the right one, or both – Alexander Zhak Sep 11 '14 at 18:49
0

Something Similar to below should work

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:text="New Text"
            android:id="@+id/textView3" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:text="New Text"

            android:layout_gravity="end"
            android:id="@+id/textView4" />
    </RelativeLayout>
Kalel Wade
  • 7,742
  • 3
  • 39
  • 55