Tyrannoseanus
Tyrannoseanus

Reputation: 4314

Stretching a button vertically in a ListView

I'm working on using a custom ListView that uses an inline delete button, but can't get the button to stretch vertically when the ListView row goes over 1 line.

I'm also having a bit of trouble with getting the text to respect the boundaries of the button, though that was working earlier so I suspect that to be an easy fix.

The code I am using is as follows:

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

 <TextView 
    android:id="@+id/txt_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:gravity="center_vertical"
    android:textStyle="bold"
    android:textSize="22sp"
    android:textColor="#000000"
    android:textIsSelectable="false"
    android:layout_margin="8dp" />

 <Button 
     android:id="@+id/btn_delete"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_alignParentRight="true"
     android:background="@drawable/delete"
     android:contentDescription="@string/delete"/>

</RelativeLayout>

And it comes out looking like so:

My ListView

Upvotes: 0

Views: 165

Answers (3)

Fahad Ishaque
Fahad Ishaque

Reputation: 1926

Add this property for button

android:scaleType="fitXY"

The image view is stretching but the image it hold do not. Please change

android:background="@drawable/delete" 

to

android:src="@drawable/delete"

Upvotes: 1

Anand
Anand

Reputation: 2885

try this one :

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

 <TextView 
    android:id="@+id/txt_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:textStyle="bold"
    android:textSize="22sp"
    android:textColor="#000000"
    android:textIsSelectable="false"
    android:layout_margin="8dp" 
    android:layout_weight="1"
    android:singleLine="false"
    android:text=""/>

 <Button 
     android:id="@+id/btn_delete"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentRight="true"
     android:background="@drawable/ic_launcher"
     android:contentDescription="@string/delete"/>

</LinearLayout>

Upvotes: 1

Md Abdul Gafur
Md Abdul Gafur

Reputation: 6201

You need to set button is also

     android:layout_centerVertical="true"


<Button 
     android:id="@+id/btn_delete"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_alignParentRight="true"
   android:layout_centerVertical="true"
     android:background="@drawable/delete"
     android:contentDescription="@string/delete"/>

I think it help you.

Upvotes: 0

Related Questions