0

I have a ListView cell with a TextView and an ImageView. I would like the ImageView to stay on the right side and the TextView width to be "the cell width minus all space taken up by the ImageView". Right now I am using this:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="@dimen/defaultCellHeight">

        <TextView
            android:id="@+id/randomSettingShow"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/defaultCellHeight"
            android:paddingLeft="30dp"
            android:textSize="@dimen/defaultTextSize"
            android:focusable="false"
            android:textStyle="normal"
            android:gravity="center_vertical|left"
            android:layout_alignParentLeft="true" />

         <ImageView
            android:id="@+id/randomSettingShowCheck"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/defaultCellHeight"
            android:gravity="center"
            android:layout_marginRight="15dp"
            android:layout_alignParentRight="true"
            android:contentDescription="@string/checkmark" />
</RelativeLayout>

But the problem is that if the text in the TextView is long enough, the text goes under the ImageView and obviously, this is incorrect. I would like the TextView to have a max width set to the left border of the ImageView.

How can I do that?

shiznatix
  • 1,087
  • 1
  • 20
  • 37

2 Answers2

1

You can try using TextView with compound drawable set - see documentation

Asahi
  • 13,378
  • 12
  • 67
  • 87
0

You can try something like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_toLeftOf="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text"
        android:id="@+id/textView"
        android:layout_alignParentLeft="true" />

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true" />
</RelativeLayout>

Like Asahi said, a compound drawable will probably be better suited for your case. Take a look at this answer here.

Community
  • 1
  • 1
Emmanuel
  • 13,083
  • 4
  • 39
  • 53