Late to the party, but here is a simplification of @Tarek360's Kotlin answer that caters for RadioGroup
s that might contain non-RadioButtons:
val RadioGroup.checkedIndex: Int
get() = children
.filter { it is RadioButton }
.indexOfFirst { it.id == checkedRadioButtonId }
If you're RadioFroup
definitely only has RadioButton
s then this can be a simple as:
private val RadioGroup.checkedIndex =
children.indexOfFirst { it.id == checkedRadioButtonId }
Then you don't have the overhead of findViewById
.