Long story short. I am creating radio buttons inside radio group. I will do filtering for my table depending on what user selects. I have one radio button checked by default. So when I try to check other button it stays checked. In fact both buttons get checked. It is so weird. Maybe that is happens because I do everything programmatically. Desing is still in progress. I just stoped here because I don't know if it is visual bug or not. I am using Kotlin for android developemnt.
Display buttons:
private fun displayChoices(choices: List<FiltersList.Choice>, multipleChoice: Boolean) {
val radioGroup = RadioGroup(this)
for (choice in choices) {
val button = if (multipleChoice) {
CheckBox(this)
} else {
RadioButton(this)
}
button.apply {
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT).apply {
setMargins(16, 8, 16, 8)
}
text = choice.display
isChecked = choice.selected
setTextColor(ResourcesCompat.getColor(resources, R.color.colorTextClicked, null))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
buttonTintList = ColorStateList.valueOf(ResourcesCompat.getColor(resources, R.color.colorButton, null))
}
}
radioGroup.addView(button)
}
filters_content.addView(radioGroup)
}
My layout:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/colorFilterBoxBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FilterDialogActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/filters_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
The view:
I select Any Date button programmatically and it is stay selected. I get data from the server which items need to be seleced by default.
Ok so at first I thought it was because you were setting other views inside the radioGroup, as radioButtons need to be a direct child. But that wasn't the case here, The issue is when creating the radioButtons inside a radioGroup programmatically you need to assign specific id's to each radioButton.
Didn't find a doc, but I am guessing a radioGroup makes use of the button ids to make the mutually exclusive. So simple solution set an id
to each radioButton you are creating
As for the id you use this is what the doc mentions.
The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.
If its in Java you could use button.setId(choices.indexOf(choice)+1001);
. I'm not so good with Kotlin but i guess the Kotlin equivalent would be
id = choices.indexOf(choice) + 1001 //where 1001 i just a random int I used to try avoid conflict
Set an id for the buttons and that should solve your issue. Good Luck.