I was wondering how to setError
for a RadioGroup
[gender] with RadioButton
's [gender_b,gender_c] if it is null.
This is how i'm getting the RadioButton
values though:
private boolean genradiocheck() {
boolean gender_flag = false;
if (gender.getCheckedRadioButtonId() == -1) {
} else {
gender_b = (RadioButton) findViewById(gender
.getCheckedRadioButtonId());
System.out.println("*********************" + gender_b.getText());
gender_flag = true;
}
return gender_flag;
}
Now my question is, how do i put it into a string and check for null values?
This is for a Registration form validation. Thank you.
By default, the RadioButton in OFF(Unchecked) state but we can change the default state of RadioButton by using android:checked attribute. Click on File,then New => New Project. Then, check Include Kotlin Support and click next button. Select minimum SDK, whatever you need.
Checking Current State Of Radio Button: 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.
here you go. Use getCheckedRadioButtonId() method on your RadioGroup to find out. It returns -1 when no RadioButton in the group is selected. You are already doing this.
setError()
method is not available for RadioGroup
but you can apply it for RadioButton
.
Follow these steps:
Make RadioButton
object and initialise it with last item of RadioGroup
.
RadioButton lastRadioBtn = (RadioButton) view.findViewById(R.id.lastRadioBtn);
Code below check if RadioGroup
had been checked and also sets error if not.
if(group.getCheckedRadioButtonId()<=0){//Grp is your radio group object
lastRadioBtn.setError("Select Item");//Set error to last Radio button
}
EDIT:
If you wish to clear Error:
lastRadioBtn.setError(null);
Hope this will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With