-3

How I know RadioButton is checked or not checked?

xml file:

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

java file:

 public class Setting extends ActionBarActivity {

        @Override

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_setting);
        }
    }
m.asadi
  • 47
  • 2
  • 13

2 Answers2

0

If you want to check on just one RadioButton you can use the isChecked function

if(radioButton.isChecked())
{
 \\ is checked    
}
else
{
 \\ not checked
}

and if you have a RadioGroup you can use

 if (radioGroup.getCheckedRadioButtonId() == -1)
{
\\ no radio buttons are checked
}
 else
   {
 \\ one of the radio buttons is checked
  }
Muhammad Younas
  • 1,543
  • 1
  • 22
  • 32
0

radiobutton.isChecked() will give you boolean and you can check it in condition .

<RadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="check"
    android:text="RadioButton" />


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setting);

    RadioButton rbutton=(RadioButton) findViewById(R.id.radioButton);
 }

public void check(View v)
{ 
    if ( radioButton.isChecked() )
    {
        //do something    
    }
    else
    {
        //do something
    }
}
CaptJak
  • 3,592
  • 1
  • 29
  • 50
AlinaBM
  • 243
  • 3
  • 12