views:

770

answers:

2

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

  • On the left: one main icon on which takes the whole height of the row
  • On the middle: 2 texts, one below the other
  • On the right: 2 icons, one below the other

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>

+2  A: 

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.

CommonsWare
Hello, yes that's a good point you'r rigth. As I am not this familiar with android layout, I've just choosen LinearLayout because it seemed simpler, but I'll definitively have a closer look to RelativeLayout. Thanks a lot, Luc
Luc
+3  A: 

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.)

Michael Cramer
Thanks a lot, I have added: android:layout_width="wrap_content" (does not work with fill_parent) android:gravity="right"Luc
Luc
Of course. Fixed, above.
Michael Cramer