How to un-check radio button without radio group?
Asked Answered
S

3

5

I have used the following code snippet to set one radio button to unchecked if the other is selected,but when I run the application and select both radio buttons the code doesn't work as both stay selected.

I'm using a relative layout so I can't use any other solutions with a radio group,I'm just using two seperate radio buttons.

Can someone point out where I could be going wrong somewhere as I can't see a flaw in the logic,any ideas?

Anyways this is the solution I'm trying to implement but its not working in application:

public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.radBtnMM:
                if (checked)
                    //set inch button to unchecked
                     radioInch.setChecked(false);
                break;
            case R.id.radBtnInch:
                if (checked)
                    //set MM button to unchecked
                    radioMM.setChecked(false);
                break;
        }
    }
Salmonella answered 11/1, 2014 at 12:4 Comment(0)
D
8

When is Your public void onRadioButtonClicked(View view) called? Is it from a listener?

This will work:

layout.xml:

<RadioButton 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radBtnMM"
/>
<RadioButton 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radBtnInch"
/>

YourActivity.java:

public class MainActivity extends Activity implements OnCheckedChangeListener {
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    setContentView(R.layout.activity_main);
    rb1 = (RadioButton) findViewById(R.id.radBtnInch);
    rb1.setOnCheckedChangeListener(this);
    rb2 = (RadioButton) findViewById(R.id.radBtnMM);
    rb2.setOnCheckedChangeListener(this);
    return true;
  }
  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
      if (buttonView.getId() == R.id.radBtnInch) {
        rb2.setChecked(false);
      }
      if (buttonView.getId() == R.id.radBtnMM) {
        rb1.setChecked(false);
      }
    }
  }
Dree answered 11/1, 2014 at 13:0 Comment(3)
I tried this fix but I'm getting the following errors where the radio button are set to this: The method setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener) in the type CompoundButton is not applicable for the arguments (MainActivity)Salmonella
Sorry, I forgot that Your Activity must implement OnCheckedChangeListener I've added this line to my response: public class MainActivity extends Activity implements OnCheckedChangeListener {Concubine
The problem was that I was using a listener method without setting the listeners to the radio buttons,thanks for the solution.Salmonella
F
2

Try this...

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch (view.getId()) {
    case R.id.radioBtnMM:
        // set inch button to unchecked
        radioBtnInch.setChecked(!checked);
        break;
    case R.id.radioBtnInch:
        // set MM button to unchecked
        radioBtnMM.setChecked(!checked);
        break;
    }
}
Fifth answered 11/1, 2014 at 12:24 Comment(0)
M
0

Try This:

@Override
public void onClick(View v)
{
    switch (v.getId()) 
    {
        case R.id.radBtnMM:
                if (checked)
                 {
                    radioInch.setChecked(false);
                 }
                 else
                 {
                   radioInch.setChecked(true);   
                 }
                break;
            case R.id.radBtnInch:
               if (checked)
                 {
                     radioMM.setChecked(false);
                 }
                 else
                 {
                    radioMM.setChecked(true);
                 }
                 break;
        }
    }
Meredeth answered 11/1, 2014 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.