Luc
Luc

Reputation: 17072

android, cannot align icons on the right in listview's row

I am currently working on my first android app. In a listview, I need to have the following organisation in each row:

I end up with a row.xml file like this, but that does not work as I expect. Do you know if something obvious is missing in this file ? Thanks a lot, Luc

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
    android:id="@+id/icon"
    android:layout_width="48px"
    android:layout_height="48px"
    android:layout_marginRight="0dip"
    android:src="@drawable/icon" />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="16dp"
        android:textStyle="bold"
        android:text="TITLE"
        android:paddingLeft="5dp"
    />
    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="14dp"
        android:text="DESCRIPTION"
        android:paddingLeft="5dp"
    />
</LinearLayout>
<LinearLayout
    android:orientation="vertical"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="right">
    <ImageButton
    android:id="@+id/button_modify"
    android:layout_width="16px"
    android:layout_height="16px"
    android:src="@drawable/deleteicon"
    />
    <ImageButton
    android:id="@+id/button_delete"
    android:layout_width="16px"
    android:layout_height="16px"
    android:src="@drawable/modifyicon"
    />
</LinearLayout>

Upvotes: 2

Views: 8756

Answers (2)

Michael Cramer
Michael Cramer

Reputation: 5230

In the right-hand-column LinearLayout, add

android:layout_width="wrap_content"
android:gravity="right"

That should make the icons should fall to the right (because of the gravity setting.)

Upvotes: 6

CommonsWare
CommonsWare

Reputation: 1006614

Consider switching to RelativeLayout for a rule set like you describe. Using LinearLayouts may not work in all cases and will be less efficient than the RelativeLayout in terms of stack consumption.

Upvotes: 4

Related Questions