Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a radiobutton is checked in a radiogroup in Android?

I need to set validation that user must fill / select all details in a page. If any fields are empty wanna show Toast message to fill. Now I need set validation for RadioButton in the RadioGroup. I tried this code but didn't work properly. Suggest me correct way. Thankyou.

// get selected radio button from radioGroup int selectedId = gender.getCheckedRadioButtonId(); // find the radiobutton by returned id selectedRadioButton = (RadioButton)findViewById(selectedId); // do what you want with radioButtonText (save it to database in your case) radioButtonText = selectedRadioButton.getText().toString();  if(radioButtonText.matches("")) {     Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();     Log.d("QAOD", "Gender is Null"); } else {     Log.d("QAOD", "Gender is Selected"); } 
like image 948
Srihari Avatar asked Jul 28 '14 10:07

Srihari


People also ask

How can I check if a RadioButton is checked in swing?

radioButton. isChecked() function returns true if the Radion button is chosen, false otherwise.

How do you programmatically determine whether a RadioButton is checked?

You can check the current state of a radio button programmatically by using isChecked() method. This method returns a Boolean value either true or false. if it is checked then returns true otherwise returns false.

How do you check if a radio button is checked or not in WPF?

The IsChecked property of RadioButton indicates whether a RadioButton is checked. The following code snippet on a button click event handler finds the text of the selected RadioButton in a group.

How can I get RadioButton text in Android?

To get the selected radio button, we have used radioGroup. getCheckedRadioButtonId() method, which returns the id of the selected radio button. Then to get the text of the selected radio button, we have used getText() method on that selected radio button.


1 Answers

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 } 
like image 80
Apoorv Avatar answered Sep 24 '22 03:09

Apoorv