Reputation: 21937
How do I ensure that user is checked at least one radiobutton from each radiogroup. Like I have this radiogroup:
<RadioGroup
android:id="@+id/myradio_group_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="third" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fourt" />
</RadioGroup>
<RadioGroup
android:id="@+id/myradio_group_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="third" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fourt" />
</RadioGroup>
I want to check programmatically if user select at least one radiobutton or not?
Upvotes: 6
Views: 23424
Reputation: 74
You can use this method
radioGroup.getCheckedRadioButtonId();
It returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.
As mentioned in the documentation https://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId()
Upvotes: 1
Reputation: 3369
if (radioGroup.getCheckedRadioButtonId() != -1)
{
// hurray at-least on radio button is checked.
}
else
{
// pls select at-least one radio button.. since id is -1 means no button is check
}
Upvotes: 5
Reputation: 151
Get a reference of the RadioGroup
and then call getCheckedRadioButtonId()
on the radio group if nothing is selected then the call will return -1
.
See public int getCheckedRadioButtonId ()
Upvotes: 15
Reputation: 1036
This is the code i have used in one of my quiz app.. Have a look at the code if this helps..
private boolean checkAnswer(){
String answer = getSelection();
if(answer==null) {
Log.d("Questions", "No checkbox selected");
return false;
}
else {
// Do your stuff here
return true;
}
}
private String getSelection(){
if (c1.isChecked())
{
return c1.getText().toString();
}
if (c2.isChecked())
{
return c2.getText().toString();
}
if (c3.isChecked())
{
return c3.getText().toString();
}
if (c4.isChecked())
{
return c4.getText().toString();
}
return null;
}
Upvotes: 2
Reputation: 6533
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);
// Returns an integer which represents the selected radio button's ID
int selected = g.getCheckedRadioButtonId();
// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);
// Now you can get the text or whatever you want from the "selected" radio button
b.getText();
Upvotes: 8