1. Are you ready for the Galaxy S20? Here is everything we know so far!

How could I use multiple radiogroup in an app?

Discussion in 'Android Development' started by RaoulH, May 8, 2020.

  1. RaoulH

    RaoulH Newbie
    Thread Starter

    Hello,
    in my app, i want to use 4 radiogroup (each radiogroup contains 2 radiobuttons):
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <ScrollView
    3.     xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:app="http://schemas.android.com/apk/res-auto"
    5.     xmlns:tools="http://schemas.android.com/tools"
    6.     android:layout_width="match_parent"
    7.     android:layout_height="match_parent">
    8.  
    9.     <androidx.constraintlayout.widget.ConstraintLayout
    10.         android:layout_width="match_parent"
    11.         android:layout_height="wrap_content">
    12.  
    13.  
    14.         <RadioGroup
    15.             android:id="@+id/estate_type_radio_group_1"
    16.             android:layout_width="0dp"
    17.             android:layout_height="wrap_content"
    18.             android:layout_marginStart="8dp"
    19.             android:layout_marginLeft="8dp"
    20.             android:layout_marginTop="16dp"
    21.             android:layout_marginEnd="8dp"
    22.             android:layout_marginRight="8dp"
    23.             android:checkedButton="@+id/filter_estate_type_a"
    24.             android:orientation="horizontal"
    25.             app:layout_constraintEnd_toEndOf="parent"
    26.             app:layout_constraintStart_toStartOf="parent"
    27.             app:layout_constraintTop_toBottomOf="@+id/sleeping_room_range_seek_bar">
    28.  
    29.             <RadioButton
    30.                 android:id="@+id/filter_estate_type_h"
    31.                 style="@style/EstateTypeRadioButtonStyle"
    32.                 android:layout_width="0dp"
    33.                 android:layout_height="match_parent"
    34.                 android:layout_margin="8dp"
    35.                 android:layout_weight="1"
    36.                 android:drawableStart="@drawable/house_with_state_48"
    37.                 android:drawableLeft="@drawable/house_with_state_48"
    38.                 android:text="@string/house" />
    39.  
    40.             <RadioButton
    41.                 android:id="@+id/filter_estate_type_a"
    42.                 style="@style/EstateTypeRadioButtonStyle"
    43.                 android:layout_width="0dp"
    44.                 android:layout_height="match_parent"
    45.                 android:layout_margin="8dp"
    46.                 android:layout_weight="1"
    47.                 android:drawableStart="@drawable/apartment_with_state_48"
    48.                 android:drawableLeft="@drawable/apartment_with_state_48"
    49.                 android:text="@string/apartment" />
    50.  
    51.         </RadioGroup>
    52.  
    53.         <RadioGroup
    54.             android:id="@+id/estate_type_radio_group_2"
    55.             android:layout_width="0dp"
    56.             android:layout_height="wrap_content"
    57.             android:layout_marginBottom="16dp"
    58.             android:orientation="horizontal"
    59.  
    60.             app:layout_constraintEnd_toEndOf="@+id/estate_type_radio_group_1"
    61.             app:layout_constraintStart_toStartOf="@+id/estate_type_radio_group_1"
    62.             app:layout_constraintTop_toBottomOf="@+id/estate_type_radio_group_1"
    63.             >
    64.  
    65.             <RadioButton
    66.                 android:id="@+id/filter_estate_type_s"
    67.                 style="@style/EstateTypeRadioButtonStyle"
    68.                 android:layout_width="0dp"
    69.                 android:layout_height="match_parent"
    70.                 android:layout_margin="8dp"
    71.                 android:layout_weight="1"
    72.                 android:drawableStart="@drawable/studio_with_state_48"
    73.                 android:drawableLeft="@drawable/studio_with_state_48"
    74.                 android:text="@string/studio" />
    75.  
    76.             <RadioButton
    77.                 android:id="@+id/filter_estate_type_r"
    78.                 style="@style/EstateTypeRadioButtonStyle"
    79.                 android:layout_width="0dp"
    80.                 android:layout_height="match_parent"
    81.                 android:layout_margin="8dp"
    82.                 android:layout_weight="1"
    83.                 android:drawableStart="@drawable/sleeping_room_with_state_48"
    84.                 android:drawableLeft="@drawable/sleeping_room_with_state_48"
    85.                 android:text="@string/sleeping_room" />
    86.  
    87.         </RadioGroup>
    88.  
    89.  
    90.         <RadioGroup
    91.             android:id="@+id/estate_type_radio_group_3"
    92.             android:layout_width="0dp"
    93.             android:layout_height="wrap_content"
    94.             android:layout_marginBottom="16dp"
    95.             android:orientation="horizontal"
    96.  
    97.             app:layout_constraintEnd_toEndOf="@+id/estate_type_radio_group_2"
    98.             app:layout_constraintStart_toStartOf="@+id/estate_type_radio_group_2"
    99.             app:layout_constraintTop_toBottomOf="@+id/estate_type_radio_group_2">
    100.  
    101.             <RadioButton
    102.                 android:id="@+id/filter_estate_type_local_c"
    103.                 style="@style/EstateTypeRadioButtonStyle"
    104.                 android:layout_width="0dp"
    105.                 android:layout_height="match_parent"
    106.                 android:layout_margin="8dp"
    107.                 android:layout_weight="1"
    108.                 android:drawableStart="@drawable/local_commel_with_state_48"
    109.                 android:drawableLeft="@drawable/local_commel_with_state_48"
    110.                 android:text="@string/local_commercial" />
    111.  
    112.             <RadioButton
    113.                 android:id="@+id/filter_estate_type_apps"
    114.                 style="@style/EstateTypeRadioButtonStyle"
    115.                 android:layout_width="0dp"
    116.                 android:layout_height="match_parent"
    117.                 android:layout_margin="8dp"
    118.                 android:layout_weight="1"
    119.                 android:drawableStart="@drawable/appartement_with_state_48"
    120.                 android:drawableLeft="@drawable/appartement_with_state_48"
    121.                 android:text="Appartements meubles"/>
    122.  
    123.         </RadioGroup>
    124.         <RadioGroup
    125.             android:id="@+id/estate_type_radio_group_4"
    126.             android:layout_width="0dp"
    127.             android:layout_height="wrap_content"
    128.             android:layout_marginBottom="16dp"
    129.             android:orientation="horizontal"
    130.             app:layout_constraintBottom_toBottomOf="parent"
    131.             app:layout_constraintEnd_toEndOf="@+id/estate_type_radio_group_3"
    132.             app:layout_constraintStart_toStartOf="@+id/estate_type_radio_group_3"
    133.             app:layout_constraintTop_toBottomOf="@+id/estate_type_radio_group_3"
    134.             >
    135.  
    136.             <RadioButton
    137.                 android:id="@+id/filter_estate_ty"
    138.                 style="@style/EstateTypeRadioButtonStyle"
    139.                 android:layout_width="0dp"
    140.                 android:layout_height="match_parent"
    141.                 android:layout_margin="8dp"
    142.                 android:layout_weight="1"
    143.                 android:drawableStart="@drawable/ter_with_state_48"
    144.                 android:drawableLeft="@drawable/ter_with_state_48"
    145.                 android:text="Terrain" />
    146.  
    147.             <RadioButton
    148.                 android:id="@+id/filter_estate_type_roo"
    149.                 style="@style/EstateTypeRadioButtonStyle"
    150.                 android:layout_width="0dp"
    151.                 android:layout_height="match_parent"
    152.                 android:layout_margin="8dp"
    153.                 android:layout_weight="1"
    154.                 android:visibility="invisible"
    155.                 android:drawableStart="@drawable/sleeng_rm_with_state_48"
    156.                 android:drawableLeft="@drawable/sleeng_rm_with_state_48"
    157.                 android:text="@string/sleeping_room" />
    158.  
    159.         </RadioGroup>
    160.  
    161.  
    162.            </androidx.constraintlayout.widget.ConstraintLayout>
    163. </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:
    Code (Java):
    1. isChecking =true;
    2. mEstateTypeGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    3.             @Override
    4.             public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
    5.                 if (checkedId != -1 && isChecking) {
    6.                     isChecking = false;
    7.                     mEstateTypeGroup2.clearCheck();
    8.                     mEstateTypeGroup3.clearCheck();
    9.                     mEstateTypeGroup4.clearCheck();
    10.                     mEstateTypeCheckedId = checkedId;
    11.                 }
    12.                 isChecking = true;
    13.             }
    14.         });
    15.         mEstateTypeGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    16.             @Override
    17.             public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
    18.                 if (checkedId != -1 && isChecking) {
    19.                     isChecking = false;
    20.                     mEstateTypeGroup1.clearCheck();
    21.                     mEstateTypeGroup3.clearCheck();
    22.                     mEstateTypeGroup4.clearCheck();
    23.                     mEstateTypeCheckedId = checkedId;
    24.                 }
    25.                 isChecking = true;
    26.             }
    27.         });
    28.  
    29.         mEstateTypeGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    30.             @Override
    31.             public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
    32.                 if (checkedId != -1 && isChecking) {
    33.                     isChecking = false;
    34.                     mEstateTypeGroup1.clearCheck();
    35.                     mEstateTypeGroup2.clearCheck();
    36.                     mEstateTypeGroup4.clearCheck();
    37.                     mEstateTypeCheckedId = checkedId;
    38.                  }
    39.                 isChecking = true;
    40.             }
    41.         });
    42.  
    43.         mEstateTypeGroup4.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    44.             @Override
    45.             public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
    46.                 if (checkedId != -1 && isChecking) {
    47.                     isChecking = false;
    48.                     mEstateTypeGroup1.clearCheck();
    49.                     mEstateTypeGroup2.clearCheck();
    50.                     mEstateTypeGroup3.clearCheck();
    51.                     mEstateTypeCheckedId = checkedId;
    52.                     }
    53.  
    54.                 isChecking = true;
    55.             }
    56.         });
    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
     


    #1 RaoulH, May 8, 2020
    Last edited: May 8, 2020

    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - multiple radiogroup app
  1. Daical
    Replies:
    1
    Views:
    238
  2. user_2014
    Replies:
    0
    Views:
    278
  3. tknChris
    Replies:
    2
    Views:
    349
  4. Kerrypatrick
    Replies:
    0
    Views:
    339
  5. yule dee
    Replies:
    2
    Views:
    518
  6. steve reine
    Replies:
    0
    Views:
    557
  7. doclobot
    Replies:
    3
    Views:
    530
  8. sadams54
    Replies:
    1
    Views:
    255
  9. plynius
    Replies:
    1
    Views:
    850
  10. Brandyn Michael Scott
    Replies:
    0
    Views:
    191

Share This Page

Loading...