Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check and uncheck single radio button

I have an issue like selecting and unselecting a single radio button on click, the setOnCheckChangeListner() works only for the first time below is the code which I have tried.

radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               if(isChecked) radioButton.setChecked(false);
               else radioButton.setChecked(true);
           }
       });
like image 351
Vikas Jain Avatar asked Dec 14 '22 15:12

Vikas Jain


2 Answers

I have made a solution for it using set selected attribute of the radio button.

final RadioButton radioButton = findViewById(R.id.radioButton);
        radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!radioButton.isSelected()) {
                    radioButton.setChecked(true);
                    radioButton.setSelected(true);
                } else {
                    radioButton.setChecked(false);
                    radioButton.setSelected(false);
                }
            }
        });
like image 120
avez raj Avatar answered Dec 16 '22 05:12

avez raj


try to use preference xml this one is essay to save the click events.because its like small db.
How to create RadioButton group in preference.xml window?

like image 24
suresh madaparthi Avatar answered Dec 16 '22 05:12

suresh madaparthi