Frage

I am trying to generate radio button in a radio Group dynamically. I have a radioGroup having three radioButtons. On runtime I want to put a validation that if no radio Button is checked, set a background color of radioGroup to red indicating warning.

For the purpose I am using

public String validate() {
    if (radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()) != null){
        return "SUCCESS";
    } else {
        radioGroup.setBackgroundResource(R.drawable.red_border);
        return "failure";
    }

}

but here the background is not changing for radioGroup i have tried similar approach for editText and DatePicker in all that it's working.

War es hilfreich?

Lösung

We can do this using looping through radioGroup children

for (int i = 0; i < radioGroup.getChildCount(); ++i) {
    ((RadioButton) radioGroup.getChildAt(i))
    .setBackgroundResource(R.drawable.red_border);
}

This will loop through each and every children of RadioGroup and like this we can achieve the desired functionality

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top