Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RadiGroup allows multiple selection

Android API Level 24 (emulator) seems to allow multiple selection if I pre-select multiple RadioButton initially. I just want to know if this is a bug or not?

Here is the layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/metal"
           android:checked="true"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/classical"
            android:checked="true"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/jazz"/>

    </RadioGroup>

</LinearLayout>

The app launches like this:

enter image description here

And if I click on Jazz, it becomes like this:

enter image description here

like image 608
Mert Akcakaya Avatar asked May 31 '26 20:05

Mert Akcakaya


1 Answers

Your layout lacks android:id values for the RadioButton widgets. That can work, if you're not starting with any of them pre-checked in the layout resource. If you are going to use android:checked in the layout resource, you need to assign widget IDs to the RadioButton widgets. This is a long-standing issue that is unlikely to change, so "it's just one of those things" that we have to deal with in Android app development.

like image 179
CommonsWare Avatar answered Jun 03 '26 10:06

CommonsWare