Enable and disable RadioGroup : RadioGroup « UI « Android






Enable and disable RadioGroup

  
package app.test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

public class Test extends Activity {
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.testRadioGroup);

    final Button changeButton = (Button) findViewById(R.id.enableButton);
    changeButton.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        changeOption(radiogroup);
      }
    });
    final Button changeButton2 = (Button) findViewById(R.id.backgroundColorButton);
    changeButton2.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        changeOption2(radiogroup);

      }
    });
  }

  public void changeOption(RadioGroup radiogroup) {
    if (radiogroup.isEnabled()) {
      radiogroup.setEnabled(false);
    } else {
      radiogroup.setEnabled(true);

    }
  }

  public void changeOption2(RadioGroup radiogroup) {
    radiogroup.setBackgroundColor(Color.RED);
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<RadioGroup android:id="@+id/testRadioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
       <RadioButton
            android:text="Radio 1"
            android:id="@+id/radio1"
            />
        <RadioButton
            android:text="Radio 2"
            android:id="@+id/radio2" />
 </RadioGroup>
<Button android:id="@+id/enableButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set isEnabled"/>
<Button android:id="@+id/backgroundColorButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Background Color"/>
</LinearLayout>

   
    
  








Related examples in the same category

1.Add a RadioGroup
2.Adding Check listener to RadioGroup
3.RadioGroup selection Changed Listener
4.Clearing the selection for RadioGroup
5.Create RadioGroup
6.Listening to checked change events