15

I'm really pulling my hair out over this one. Some background. I have a list of items that all have checkboxes next to them. When you deselect a checkbox, a button appears that allows you to delete the item from the list. This seems backwards at first but we only want "selected" items to be eligible for further processing, etc. This is my layout:

<RelativeLayout android:id="@+id/rlBlahBlah" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:minHeight="?android:attr/listPreferredItemHeight"
                xmlns:android="http://schemas.android.com/apk/res/android">
  <CheckBox android:text="" 
            android:id="@+id/cbDeleteItem" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:focusable="false"
            />
  <TextView android:text="" 
            android:id="@+id/tvItemText" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:textSize="14dip"
            android:paddingLeft="3dip"
            android:paddingRight="3dip"
            android:paddingTop="13dip"
            android:gravity="fill_vertical"
            android:layout_toRightOf="@id/cbDeleteItem"
            />
  <Button android:layout_height="wrap_content" 
          android:id="@+id/btnDelete" 
          android:text="Delete" 
          android:layout_width="wrap_content"
          android:layout_alignParentRight="true"
          android:gravity="center_vertical"
          android:focusable="false"
          />
</RelativeLayout>

I cannot get the 3 items to center vertically in my row to save my life. layout_gravity, gravity, layout_centerVertical, none of it works. I'm sure my issue is some tiny setting to flip somewhere, but I'm really at wits end on this.

edit: I know the textview is "fill_vertical", that's some random stuff I was trying.

MattC
  • 12,285
  • 10
  • 54
  • 78
  • Have you tried playing around with applying gravity attributes to the parent RelativeLayout view? Worth a shot. – Eric Mill Sep 30 '09 at 19:17

6 Answers6

59

This attribute worked for me centering a Button using RelativeLayout:

android:layout_centerInParent="true"

See this posting: Can you center a Button in RelativeLayout?

Community
  • 1
  • 1
John
  • 989
  • 2
  • 9
  • 11
24

Your problem is probably not due to the layout, but how you are inflating the layout. In fact, it might even be my fault, depending on where you learned your technique...

To quote the omnipresent Romain Guy:

the correct usage of inflate() in adapters is:

inflate(layoutId, parent, false);

Passing the parent (given to you as a parameter in getView()) allows the UI toolkit to create the appropriate LayoutParams object. Passing false tells the toolkit to NOT call parent.addView(theInflateChild), since ListView will do its own magic later on.

If you use inflate(layoutId, null), as I have traditionally advised, life is OK unless you try using RelativeLayout as the base layout of the rows and try to use vertical centering.

I will be updating my books and such to reflect the new advice in the coming weeks.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am in fact using your technique :P I also read a post from Romain Guy stating that RelativeLayout ignores layout_gravity attributes. I'm posting an answer with the new layout that does exactly what I want. Thank you for your response, though! – MattC Oct 01 '09 at 13:14
16

Using RelativeLayout, I had no luck with gravity nor layout_gravity.

But android:layout_centerVertical="true" worked for me positioned in the child of my RelativeLayout, so you can keep it if you need.

Murphy
  • 4,858
  • 2
  • 24
  • 31
2

Here is a layout that ended up doing exactly what I wanted. I ditched RelativeLayout once I learned that it ignores layout_gravity attributes (of course now I can't find the post). Either way, nested LinearLayouts did the trick for me:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:id="@+id/llBlahBlahRowMain"
    android:padding="6dip">
    <CheckBox android:text="" 
              android:id="@+id/cbDeleteItem" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical"
              android:focusable="false"
              />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:layout_height="wrap_content">
         <TextView 
            android:text="blah blah dynamically replaced text" 
            android:id="@+id/tvItemText" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_weight="1"
            android:textSize="14dip"
            android:paddingLeft="3dip"
            android:layout_gravity="center_vertical"
            />
    </LinearLayout>
    <Button android:layout_width="wrap_content"
          android:layout_height="wrap_content" 
          android:id="@+id/btnDelete" 
          android:text="Delete" 
          android:layout_gravity="center_vertical" 
          android:focusable="false"
          />
</LinearLayout>
MattC
  • 12,285
  • 10
  • 54
  • 78
1

Have you tried android:gravity="center_horizontal" as referenced here

Carnell
  • 749
  • 6
  • 10
  • I'm looking for vertical centering within the layout. – MattC Sep 30 '09 at 17:45
  • There is also a center_vertical there. – Carnell Sep 30 '09 at 20:26
  • gravity centers items within themselves as I've found. What I wanted was layout_gravity, but according to some post by Romain Guy that I read, RelativeLayout ends up ignoring layout_gravity – MattC Oct 01 '09 at 13:13
1

I did an alternative solution, that worked for me. I put, on my textbox element, the property android:layout_height="fill_parent". This way, the text element filled all hieght of parent, so the contents were aligned correctly :)

<TextView android:id="@+id/principal_li_descricao" 
    android:layout_height="fill_parent"
    android:layout_width="wrap_content"
    android:layout_marginLeft="10dip" 
    android:textColor="#ff000000" 
    android:layout_centerVertical="true"
    android:gravity="center_vertical"
    android:layout_alignBottom="@+id/principal_li_icone"
    android:layout_toRightOf="@+id/principal_li_icone"/>

Thanks

Luiz Vianna
  • 71
  • 1
  • 1