RadioGroup empty field check using setError in Android

MetaldroiD picture MetaldroiD · Mar 6, 2014 · Viewed 14.5k times · Source

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.

Answer

Crawler picture Crawler · Aug 18, 2015

setError() method is not available for RadioGroup but you can apply it for RadioButton.

Follow these steps:

  1. Make RadioButton object and initialise it with last item of RadioGroup.

    RadioButton lastRadioBtn = (RadioButton) view.findViewById(R.id.lastRadioBtn);
    
  2. 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.