Sweeper
Sweeper

Reputation: 270770

Android Radio Button not working properly

I am trying to make a math app for little kids. And there is kind of an activity to customize the settings of the math questions. (number of digits, + or -, whether timer is enabled etc.) And I am doing the number of digits part. Here's my layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity"
                android:layout_gravity="center_horizontal"
                android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/customize_menu"
        android:layout_weight="9">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/digit_text"
                android:textSize="@dimen/customize_menu_title"/>

            <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1"
                    android:textSize="@dimen/customize_menu_radio"
                    android:layout_marginLeft="@dimen/radio_button_spacing"/>

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2"
                    android:textSize="@dimen/customize_menu_radio"
                    android:layout_marginLeft="@dimen/radio_button_spacing"
                    android:checked="true"/>

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3"
                    android:textSize="@dimen/customize_menu_radio"
                    android:layout_marginLeft="@dimen/radio_button_spacing"/>

            </RadioGroup>

        </LinearLayout>

    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/button_trophies"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/button_start"/>
    </LinearLayout>

</LinearLayout>

As you can see, I set the radio button with the text "2" to be checked by default. However, when I run the app and click on the radio button "3", both radio button "2" and "3" is checked! Then I tried to click on the radio button "1" and now only radio button "1" and "2" is checked. In other words, whatever radio button I press, radio button "2" is always checked. But that certainly isn't how radio buttons work, right?

Questions: How can I fix this behaviour without changing the fact that the the radio button "2" is checked by default? Why are they having this behaviour?

Upvotes: 0

Views: 4923

Answers (1)

Ravi
Ravi

Reputation: 35539

give id to your radiobutton and problem is solved

           <RadioButton android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                android:textSize="@dimen/customize_menu_radio"
                android:layout_marginLeft="@dimen/radio_button_spacing"/>

            <RadioButton android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:textSize="@dimen/customize_menu_radio"
                android:layout_marginLeft="@dimen/radio_button_spacing"
                android:checked="true"/>

            <RadioButton android:id="@+id/btn3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="3"
                android:textSize="@dimen/customize_menu_radio"
                android:layout_marginLeft="@dimen/radio_button_spacing"/>

Upvotes: 4

Related Questions