Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RadioGroup empty field check using setError in Android

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.

like image 955
MetaldroiD Avatar asked Mar 06 '14 04:03

MetaldroiD


People also ask

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

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.

How check radio button is clicked or not in android?

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.

How do you check whether a RadioGroup is checked or not?

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.


1 Answers

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.

like image 166
Crawler Avatar answered Sep 28 '22 21:09

Crawler