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
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (mRadioButtonMale.isChecked()) {
text = "male";
} else {
text = "female";
}
}
});
OR
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (mRadioButtonMale.isChecked()) { text = "male"; }
if(mRadioButtonFemale.isChecked()) { text = "female"; }
}
});
I used the id's of my radiobuttons to compare with the id of the checkedRadioButton
that I got using mRadioGroup.getCheckedRadioButtonId()
Here is my code:
mRadioButton1=(RadioButton)findViewById(R.id.first);
mRadioButton2=(RadioButton)findViewById(R.id.second);
mRadioButton3=(RadioButton)findViewById(R.id.third);
mRadioButton4=(RadioButton)findViewById(R.id.fourth);
mNextButton=(Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId=mRadioGroup.getCheckedRadioButtonId();
int n=0;
if(selectedId==R.id.first){n=1;}
else if(selectedId==R.id.second){n=2;}
else if(selectedId==R.id.third){n=3;}
else if(selectedId==R.id.fourth){n=4;}
//. . . .
}
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
}
All you need to do is use getCheckedRadioButtonId()
and isChecked()
method,
if(gender.getCheckedRadioButtonId()==-1)
{
Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
}
else
{
// get selected radio button from radioGroup
int selectedId = gender.getCheckedRadioButtonId();
// find the radiobutton by returned id
selectedRadioButton = (RadioButton)findViewById(selectedId);
Toast.makeText(getApplicationContext(), selectedRadioButton.getText().toString()+" is selected", Toast.LENGTH_SHORT).show();
}
https://developer.android.com/guide/topics/ui/controls/radiobutton.html
Try to use the method isChecked();
Like,
selectedRadioButton.isChecked() -> returns boolean.
Refere here for more details on Radio Button
Use the isChecked() function for every radioButton you have to check.
RadioButton maleRadioButton, femaleRadioButton;
maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButton);
femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButton);
Then use the result for your if/else case consideration.
if (maleRadioButton.isChecked() || femaleRadioButton.isChecked()) {
Log.d("QAOD", "Gender is Selected");
} else {
Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
Log.d("QAOD", "Gender is Null");
}