Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android List Item: TextView on left, Image on right

I'm creating a custom list item in and android list view. it's very simple, it has a textview on the left and an image on the right. the image should align all the way right, and the text view should take up all the space on the left. problem is, i can get the image to show up, but it pushes left. if i set the textview width to fill_parent, the image disappears. here's my layout:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" 
              android:layout_width="fill_parent" android:layout_height="50px" android:layout_gravity="center_vertical">
  <!-- item -->
  <TextView android:id="@+id/txtItem" android:gravity="center_vertical" android:textSize="20px" android:layout_width="fill_parent" 
            android:layout_height="fill_parent"/>

  <!-- disclosure image -->
  <ImageView android:layout_gravity="right|center_vertical" android:src="@drawable/common_icon_arrow_disclosure" 
              android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>

i've also tried a relative layout, but the same thing happens. what am i doing wrong?


EDIT: for clarification, this is what i'm trying to accomplish:

alt text

i want the text field to take up the whole left area, and the image to be on the right, unscaled, vertically centered.

like image 594
bryan costanich Avatar asked Dec 05 '10 02:12

bryan costanich


3 Answers

You can also just add the drawable to the textview directly like this.

<TextView
    android:id="@+id/txtItem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawableRight="@drawable/common_icon_arrow_disclosure"
    android:gravity="center_vertical"
    android:textSize="20px" />
like image 163
guyland123 Avatar answered Nov 15 '22 11:11

guyland123


You can use bitmap drawable like this (this also adds rounded corners):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@android:color/white" />
            <stroke
                android:width="2dp"
                android:color="#adadad"
            />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item android:right="5dp"
        android:top="2dp"
        android:bottom="2dp">
        <bitmap
            android:src="@drawable/image"
            android:gravity="right"
        />
    </item>
</layer-list>

and put this drawable in background property of your TextView:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/right_image"
    android:text="some text"
    />
like image 45
tapchicoma Avatar answered Nov 15 '22 11:11

tapchicoma


Left-align your text with the parent, and set the gravity of the image to right:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="48dp">

  <TextView
  android:id="@+id/team_member_name"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_alignParentLeft="true"
  android:gravity="center_vertical"
  android:singleLine="true"/>

  <ImageView
  android:id="@+id/team_member_icon"
  android:layout_alignParentRight="true"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_gravity="right"
  android:layout_marginLeft="8dp"
  android:layout_marginRight="8dp"
  android:src="@drawable/circle_green"/>

</RelativeLayout>
like image 1
xaviert Avatar answered Nov 15 '22 13:11

xaviert