Search code examples
androidandroid-radiogroup

What should getCheckedRadioButtonId() return after RadioGroup.clearCheck()?


From the docs:

public void clearCheck ()

Clears the selection. When the selection is cleared, no radio button in this group is selected and getCheckedRadioButtonId() returns null.

But seems like it actually returns -1. Also, the line:

if(radioGroup.getCheckedRadioButtonId()==null)

gives the error:

The operator == is undefined for the argument type(s) int, null

Should I count on it returning -1 after performing clearCheck()?


Solution

  • The return type of radioGroup.getCheckedRadioButtonId() (as in the docs) is an int. An int cannot be null, so I think you should rely on -1.

    Edit: If you're not sure how an Android component works, just check the source code :) For the RadioGroup, see for example: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/RadioGroup.java#RadioGroup.clearCheck%28%29