• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How could I use multiple radiogroup in an app?

RaoulH

Newbie
Hello,
in my app, i want to use 4 radiogroup (each radiogroup contains 2 radiobuttons):
Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <RadioGroup
            android:id="@+id/estate_type_radio_group_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:checkedButton="@+id/filter_estate_type_a"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/sleeping_room_range_seek_bar">

            <RadioButton
                android:id="@+id/filter_estate_type_h"
                style="@style/EstateTypeRadioButtonStyle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:drawableStart="@drawable/house_with_state_48"
                android:drawableLeft="@drawable/house_with_state_48"
                android:text="@string/house" />

            <RadioButton
                android:id="@+id/filter_estate_type_a"
                style="@style/EstateTypeRadioButtonStyle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:drawableStart="@drawable/apartment_with_state_48"
                android:drawableLeft="@drawable/apartment_with_state_48"
                android:text="@string/apartment" />

        </RadioGroup>

        <RadioGroup
            android:id="@+id/estate_type_radio_group_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:orientation="horizontal"

            app:layout_constraintEnd_toEndOf="@+id/estate_type_radio_group_1"
            app:layout_constraintStart_toStartOf="@+id/estate_type_radio_group_1"
            app:layout_constraintTop_toBottomOf="@+id/estate_type_radio_group_1"
            >

            <RadioButton
                android:id="@+id/filter_estate_type_s"
                style="@style/EstateTypeRadioButtonStyle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:drawableStart="@drawable/studio_with_state_48"
                android:drawableLeft="@drawable/studio_with_state_48"
                android:text="@string/studio" />

            <RadioButton
                android:id="@+id/filter_estate_type_r"
                style="@style/EstateTypeRadioButtonStyle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:drawableStart="@drawable/sleeping_room_with_state_48"
                android:drawableLeft="@drawable/sleeping_room_with_state_48"
                android:text="@string/sleeping_room" />

        </RadioGroup>


        <RadioGroup
            android:id="@+id/estate_type_radio_group_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:orientation="horizontal"

            app:layout_constraintEnd_toEndOf="@+id/estate_type_radio_group_2"
            app:layout_constraintStart_toStartOf="@+id/estate_type_radio_group_2"
            app:layout_constraintTop_toBottomOf="@+id/estate_type_radio_group_2">

            <RadioButton
                android:id="@+id/filter_estate_type_local_c"
                style="@style/EstateTypeRadioButtonStyle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:drawableStart="@drawable/local_commel_with_state_48"
                android:drawableLeft="@drawable/local_commel_with_state_48"
                android:text="@string/local_commercial" />

            <RadioButton
                android:id="@+id/filter_estate_type_apps"
                style="@style/EstateTypeRadioButtonStyle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:drawableStart="@drawable/appartement_with_state_48"
                android:drawableLeft="@drawable/appartement_with_state_48"
                android:text="Appartements meubles"/>

        </RadioGroup>
        <RadioGroup
            android:id="@+id/estate_type_radio_group_4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/estate_type_radio_group_3"
            app:layout_constraintStart_toStartOf="@+id/estate_type_radio_group_3"
            app:layout_constraintTop_toBottomOf="@+id/estate_type_radio_group_3"
            >

            <RadioButton
                android:id="@+id/filter_estate_ty"
                style="@style/EstateTypeRadioButtonStyle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:drawableStart="@drawable/ter_with_state_48"
                android:drawableLeft="@drawable/ter_with_state_48"
                android:text="Terrain" />

            <RadioButton
                android:id="@+id/filter_estate_type_roo"
                style="@style/EstateTypeRadioButtonStyle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:visibility="invisible"
                android:drawableStart="@drawable/sleeng_rm_with_state_48"
                android:drawableLeft="@drawable/sleeng_rm_with_state_48"
                android:text="@string/sleeping_room" />

        </RadioGroup>


           </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
When i select a radiobutton, i should like that the others radiobuttons would uncheck even if they are in another radiogroup.
The code java that i used is here:
Java:
isChecking =true;
mEstateTypeGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (checkedId != -1 && isChecking) {
                    isChecking = false;
                    mEstateTypeGroup2.clearCheck();
                    mEstateTypeGroup3.clearCheck();
                    mEstateTypeGroup4.clearCheck();
                    mEstateTypeCheckedId = checkedId;
                }
                isChecking = true;
            }
        });
        mEstateTypeGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (checkedId != -1 && isChecking) {
                    isChecking = false;
                    mEstateTypeGroup1.clearCheck();
                    mEstateTypeGroup3.clearCheck();
                    mEstateTypeGroup4.clearCheck();
                    mEstateTypeCheckedId = checkedId;
                }
                isChecking = true;
            }
        });

        mEstateTypeGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (checkedId != -1 && isChecking) {
                    isChecking = false;
                    mEstateTypeGroup1.clearCheck();
                    mEstateTypeGroup2.clearCheck();
                    mEstateTypeGroup4.clearCheck();
                    mEstateTypeCheckedId = checkedId;
                 }
                isChecking = true;
            }
        });

        mEstateTypeGroup4.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (checkedId != -1 && isChecking) {
                    isChecking = false;
                    mEstateTypeGroup1.clearCheck();
                    mEstateTypeGroup2.clearCheck();
                    mEstateTypeGroup3.clearCheck();
                    mEstateTypeCheckedId = checkedId;
                    }

                isChecking = true;
            }
        });

But when i execute this code in my app, the selection of radiobutton doesn't work properly. For example: when i check a button and i check another button, the first button isn't unchecked.

Please could you have any idea?
Thank you
 
Last edited:
Back
Top Bottom