Alaa Khaldi
Alaa Khaldi

Reputation: 11

In ListView item, RelativeLayout child not expanding to fill free space

inside ListView item what I'm trying to do is to place an ImageView over a TextView while the TextView must fill it's parent, here's what i did:

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

    <TextView android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:background="#FFFF00" />

    <ImageView android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:background="#550000FF" />    
</RelativeLayout>

notes:

here is a screenshot: notice that the TextView is acting like it's given "wrap_content" while it should be filling all the white space.

so what am I missing here?

thanks!

Upvotes: 1

Views: 789

Answers (2)

Sumit Jain
Sumit Jain

Reputation: 1150

Change parent layout height to wrap_content it will solve your issue

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<TextView android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:background="#FFFF00" />

<ImageView android:id="@+id/icon"
    android:layout_width="50dp"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:background="#550000FF" />
</RelativeLayout>

OR You can set minHeight=100 to TextView. This will take minimum height 100dp same as ImageView and increase its height after minimum height.

<TextView android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="100dp"
    android:layout_alignParentTop="true"
    android:background="#FFFF00" />

Upvotes: 0

Libin
Libin

Reputation: 17095

Set the TextView height same as ImageView, since your using Relative Layout for the ListView item layout, the item height will be the maximum height of its inner child.

Change the Textview as

<TextView android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:background="#FFFF00" />

If , the ImageView height is not fixed, then you can set the TextView height from your adapter getView method

  textview.setHeight(parent.getMeasuredHeight());

Also , setting android:layout_height="match_parent" for a ListView item parent Layout doesn't make any sense, change it to wrap_content

Upvotes: 0

Related Questions