misguided
misguided

Reputation: 3789

Radio Button checking and unchecking programmatically in android

My requirement is to be able to be able to check only 1 radio button from the no of options available.

My layout is simple

enter image description here

User should be able to select only one of p1, p2 and p3.

The IDs for for the buttons are rB1,rB2 and rB3.

PS: I know it is possible to achieve the same with RadioGroup , but in this particular instance I want to go with Radio Button hence the query

Upvotes: 3

Views: 12840

Answers (5)

misguided
misguided

Reputation: 3789

The solution can be achieved by setting a android:onClick="onRadioButtonClick" in the layout.xml file and creating a onRadioButtonClick method in Activity.java

public void onRadioButtonClick(View v)
{
    boolean checked = ((RadioButton) v).isChecked();
    RadioButton r1  = (RadioButton) findViewById(R.id.rB1);
    RadioButton r2 = (RadioButton) findViewById(R.id.rB2);
    RadioButton r3 = (RadioButton) findViewById(R.id.rB3);
    switch(v.getId()) {     

case R.id.rB1:

    if (checked){
        r2.setChecked(false);
        r3.setChecked(false);
    }
    break;

case R.id.rB2:
    if (checked){
        r1.setChecked(false);
        r3.setChecked(false);
    }
    break;

case R.id.rB2:
    if (checked){
        r1.setChecked(false);
        r2.setChecked(false);
    }   
    break;

    }

I am posting this soulution because I couldn't find the solution to this query on this site(when I was looking for it earlier in the day) and had to figure it out myself. This might be useful for someone else looking for a similar solution.

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this..

r1.setOnCheckedChangeListener(this);
r2.setOnCheckedChangeListener(this);
r3.setOnCheckedChangeListener(this);

And CheckedChangeListener

@Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
      if (buttonView.getId() == R.id.rB1) {
        r2.setChecked(false);
        r3.setChecked(false);
      }
      if (buttonView.getId() == R.id.rB2) {
        r1.setChecked(false);
        r3.setChecked(false);
      }
      if (buttonView.getId() == R.id.rB3) {
        r1.setChecked(false);
        r2.setChecked(false);
      }
    }
  }

and don't forget to implements OnCheckedChangeListener in your activity or fragment.

Upvotes: 6

duggu
duggu

Reputation: 38429

try below code:-

java

public class MyAndroidAppActivity extends Activity {

  private RadioGroup radioSexGroup;
  private RadioButton radioSexButton;
  private Button btnDisplay;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

  }

  public void addListenerOnButton() {

    radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

    btnDisplay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                // get selected radio button from radioGroup
            int selectedId = radioSexGroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
                radioSexButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioSexButton.getText(), Toast.LENGTH_SHORT).show();

        }

    });

  }
}

xml

<?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="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />

    </RadioGroup>

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>

for more detail see below link:-

http://www.mkyong.com/android/android-radio-buttons-example/

http://www.tutorialspoint.com/android/android_radiogroup_control.htm

Upvotes: 0

Madhu
Madhu

Reputation: 298

Use Radio group

XML:

 <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/rB1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="P1" 
            android:checked="true" />

        <RadioButton
            android:id="@+id/rB2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="P2" />
       <RadioButton
            android:id="@+id/rB3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="P3" />

    </RadioGroup>

Upvotes: 0

Aman Singh
Aman Singh

Reputation: 360

Try this way:

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/p1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="P1" />

    <RadioButton
        android:id="@+id/p2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="P2" />

    <RadioButton
        android:id="@+id/p3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="P3" />
</RadioGroup>

Upvotes: 0

Related Questions