Search code examples
androidradio-buttonandroid-radiogroup

how to make a radiobutton outside the radio group uncheck automatically when radiobutton on first radiogroup is checked?


I have a case like this, I have a tablelayout that has 4 radiogroups which each radiogroup have 2 radiobutton. how do I make 4 radiogroups synchronize with each other. When the radiobutton at radiogroup 1 is checked, the radiobutton in radiogroup 2 is unchecked?

This is my XML code :

<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="wrap_content"
android:orientation="vertical">

<TextView
    android:layout_margin="20sp"
    android:textSize="25sp"
    android:fontFamily="monospace"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/judulkolomtes"/>

<TableLayout
    android:layout_margin="10sp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_white">

        <RadioGroup
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:orientation="horizontal">
        <TextView
            android:padding="10sp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/P"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textSize="18sp"
            android:textColor="@color/white"/>

        <TextView
            android:padding="10sp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/K"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textSize="18sp"
            android:textColor="@color/white"/>
        </RadioGroup>

        <TextView
            android:padding="10sp"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="@string/pernyataanhead"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="@color/white"/>

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_black">

        <RadioGroup
            android:id="@+id/Rgroup1"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/Pradio1"
                android:checked="false"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/border_black"/>

            <RadioButton
                android:id="@+id/Kradio1"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/border_black"/>
        </RadioGroup>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan1"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/border_black"
        android:orientation="horizontal">

        <RadioGroup
            android:id="@+id/Rgroup2"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:orientation="horizontal">

        <RadioButton
            android:layout_gravity="center"
            android:id="@+id/Pradio2"
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>

        <RadioButton
            android:layout_gravity="center"
            android:id="@+id/Kradio2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>
        </RadioGroup>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan2"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_black">

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

        <RadioButton
            android:id="@+id/Pradio3"
            android:layout_gravity="center"
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>

        <RadioButton
            android:id="@+id/Kradio3"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>
        </RadioGroup>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan3"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_black">

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

        <RadioButton
            android:id="@+id/Pradio4"
            android:layout_gravity="center"
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>

        <RadioButton
            android:id="@+id/Kradio4"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>
        </RadioGroup>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan4"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>
</TableLayout>

I've tried using this code :

List<RadioButton> radioButtons = new ArrayList<RadioButton>();
    radioButtons.add( (RadioButton)findViewById(R.id.Pradio1) );
    radioButtons.add( (RadioButton)findViewById(R.id.Pradio2) );
    radioButtons.add( (RadioButton)findViewById(R.id.Pradio3) );

    for (RadioButton button : radioButtons){

        button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) processRadioButtonClick(buttonView);
            }
        });
    }

private void processRadioButtonClick(CompoundButton buttonView){

    for (RadioButton button : radioButtons){

        if (button != buttonView ) button.setChecked(false);
    }

}

but the results were not as I expected. when I checked the radiobutton at radiogroup 1 then I checked radiobuton in radiogroup 2. the radiobutton in radiogroup 1 became unchecked, but when I wanted to check the radiobutton again in radiogroup 1, the radiobutton could not be checked, also with radiobutton in radiogroup 2. is there a solution to this problem? based on my analysis, making the radiobutton false when another radiobutton is checked, it causes the radiobutton to be unable to check.


Solution

  • Here is the solution.

    public class MainActivity extends AppCompatActivity {
        List<RadioButton> radioButtons;
        List<RadioGroup> radioGroups;
    
        boolean flg = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final RadioButton pradio1 = (RadioButton) findViewById(R.id.Pradio1);
            final RadioButton pradio2 = (RadioButton) findViewById(R.id.Pradio2);
            final RadioButton pradio3 = (RadioButton) findViewById(R.id.Pradio3);
            final RadioButton pradio4 = (RadioButton) findViewById(R.id.Pradio4);
            final RadioButton kradio1 = (RadioButton) findViewById(R.id.Kradio1);
            final RadioButton kradio2 = (RadioButton) findViewById(R.id.Kradio2);
            final RadioButton kradio3 = (RadioButton) findViewById(R.id.Kradio3);
            final RadioButton kradio4 = (RadioButton) findViewById(R.id.Kradio4);
            final RadioGroup rgroup1 =  (RadioGroup) findViewById(R.id.Rgroup1);
            final RadioGroup rgroup2 = (RadioGroup) findViewById(R.id.Rgroup2);
            final RadioGroup rgroup3 = (RadioGroup) findViewById(R.id.Rgroup3);
            final RadioGroup rgroup4 = (RadioGroup) findViewById(R.id.Rgroup4);
    
            radioButtons = new ArrayList<RadioButton>();
            radioButtons.add(pradio1);
            radioButtons.add(pradio2);
            radioButtons.add(pradio3);
            radioButtons.add(pradio4);
            radioButtons.add(kradio1);
            radioButtons.add(kradio2);
            radioButtons.add(kradio3);
            radioButtons.add(kradio4);
    
            radioGroups = new ArrayList<RadioGroup>();
            radioGroups.add(rgroup1);
            radioGroups.add(rgroup2);
            radioGroups.add(rgroup3);
            radioGroups.add(rgroup4);
    
            Log.e("JK->",""+ ((RadioGroup) pradio1.getParent()).getId());
    
            for (RadioButton button : radioButtons) {
                button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            Log.e("JK->","JK");
                            processRadioButtonClick(buttonView);
                        }
    
                    }
                });
            }
        }
    
        private void processRadioButtonClick(CompoundButton buttonView) {
            for (RadioGroup radioGroup: radioGroups){
                if (radioGroup.getId() != ((RadioGroup) buttonView.getParent()).getId()) {
                    radioGroup.clearCheck();
                }
            }
        }
    }
    

    What went wrong in your code.

    There is no way to uncheck RadioButton in Android. You were trying to uncheck RadioButton using setChecked(false). It was misbehaving in internal classes(RadioButton Class of Android) It was unchecking the radio button visually but not logically (My Own though from RadioButton behavior). That's why your code is not working.

    Solution Explanation.

    For solving this issue instead of unchecking RadioButton using setChecked(false) i used radioGroup.clearCheck() which uncheck both buttons(Visually and logically both) of that particular group. I am not sure that it's a clear explanation or not. Let me know if you have any questions. :)