Monday, June 18, 2012

Android: LinearLayout, Dynamically Arranging Views

Let see how to arrange buttons according to the user selection as shown below:


<LinearLayout android:orientation="vertical" android:id="@+id/linearLayout1"
     android:layout_height="fill_parent" android:layout_width="fill_parent">
    <Button android:text="Button" android:id="@+id/button0"
          android:layout_height="fill_parent" android:layout_width="fill_parent"
          android:layout_weight="1">Button>
     <Button android:text="Button" android:id="@+id/button1"
          android:layout_height="fill_parent" android:layout_width="fill_parent"
          android:layout_weight="1" android:visibility="gone">Button>
     <Button android:text="Button" android:id="@+id/button2"
          android:layout_height="fill_parent" android:layout_weight="1"
          android:layout_width="fill_parent" android:visibility="gone">Button>
LinearLayout>


Following java code would change the visibility according to the user selection.

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
     switch(group.getCheckedRadioButtonId()){
     case R.id.radio0:
          button0.setVisibility(View.VISIBLE);
          button1.setVisibility(View.GONE);
          button2.setVisibility(View.GONE);
          break;
     case R.id.radio1:
          button0.setVisibility(View.VISIBLE);
          button1.setVisibility(View.VISIBLE);
          button2.setVisibility(View.GONE);
          break;
     case R.id.radio2:
          button0.setVisibility(View.VISIBLE);
          button1.setVisibility(View.VISIBLE);
          button2.setVisibility(View.VISIBLE);
          break
      }
}

Let see if we want a layout to change as show below:
This can be down in two ways.
Option 01: changing the layout XML file to use the weight sum:

<LinearLayout android:orientation="vertical" android:id="@+id/linearLayout1"
     android:layout_height="fill_parent" android:layout_width="fill_parent" android:weightSum="3">
     <Button android:text="Button" android:id="@+id/button0"
          android:layout_width="fill_parent"
          android:layout_weight="1" android:layout_height="0dip">Button>
     <Button android:text="Button" android:id="@+id/button1"
          android:layout_width="fill_parent"
          android:layout_weight="1" android:visibility="gone" android:layout_height="0dip">        Button>
     <Button android:text="Button" android:id="@+id/button2"
          android:layout_weight="1"
          android:layout_width="fill_parent" android:visibility="gone" android:layout_height="0dip">Button>
LinearLayout>

Option 02: using java code:

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
     switch(group.getCheckedRadioButtonId()){
     case R.id.radio0:
          button0.setVisibility(View.VISIBLE);
          button1.setVisibility(View.INVISIBLE);
          button2.setVisibility(View. INVISIBLE);
          break;
     case R.id.radio1:
          button0.setVisibility(View.VISIBLE);
          button1.setVisibility(View.VISIBLE);
          button2.setVisibility(View. INVISIBLE);
          break;
     case R.id.radio2:
          button0.setVisibility(View.VISIBLE);
          button1.setVisibility(View.VISIBLE);
          button2.setVisibility(View.VISIBLE);
          break
      }
}

option 01 you should change the xml file and in option 02 you should change the java code.

1 comment:

Meghna said...

Thanks for the clean example Man... Even this http://www.compiletimeerror.com/2013/06/android-linearlayout-example.html might help.. Have a look..