RadioGroup empty field check using setError in Android
Asked Answered
D

3

14

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.

Decade answered 6/3, 2014 at 4:58 Comment(0)
M
36

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.

Maseru answered 18/8, 2015 at 14:6 Comment(2)
@dgngulcan if you need to clear setError on RadioButton use radioBtn.setError(null), this will remove errorMaseru
error mesage not dislpay , can you help me? i just see red error but can not see messageLatakia
H
3

RadioGroup doesn't support setError() method. But we can use setError() on RadioButton.

Get the position of last RadioButton in RadioGroup

int lastChildPos=radioGroup.getChildCount()-1;

then setError() on radioButton()

((RadioButton)radioGroup.getChildAt(lastChildPos)).setError("Your error");
Hautevienne answered 11/4, 2019 at 9:48 Comment(0)
B
0

Best Way to Set Error on TextView Of Radio Group Like Code Bellow textViewOfRadioGroup.setError("Please Select Item");

After This Set your Text View To Null in OnCheckedChangeListener Like this

textViewOfRadioGroup.setError(null);

Browning answered 28/7, 2022 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.