Dynamically changing layout

Posted by LTMOD on Stack Overflow See other posts from Stack Overflow or by LTMOD
Published on 2010-05-16T17:18:40Z Indexed on 2010/05/16 17:20 UTC
Read the original article Hit count: 137

Filed under:

I have a 2 x 2 grid of buttons. Each row of 2 is wrapped in a horizontal linear layout.

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="100dip">
            <Button android:layout_weight=".5"  android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_vertical|center_horizontal" android:padding="5dip" android:layout_margin="2dip">
            </Button>
            <Button android:layout_weight=".5" android:id="@+id/btn2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip" android:layout_margin="2dip">
            </Button>



At times I want to show all 4 buttons, and other times, I show the top 2 only, and sometimes, I need to show 2 on top, and one on the bottom. I would like to center the bottom one, but can't seem to accomplish this.

In my code, I call btn4.setVisibility(8); //8 = GONE, which makes my 4th button disappear, but then btn3 ends up filling the entire layout. What I would like it to do instead is center itself within the layout at its correct width / height.

I'm a bit new to this, so any help is greatly appreciated.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about android-sdk