vaniokmd
vaniokmd

Reputation: 79

RadioButton and Switch Android

I have a Switch and a RadioButton. I want that when I click and check the Switch the RadioButton will be unchecked and viceversa. How can I do this?

My code is:

 Switch switchButton = new Switch(this);
 RadioButton radioButton = new RadioButton(this);

 switchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(switchButton.isChecked()){
                radioButton.setChecked(false);
            }
        }
    });


radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(radioButton.isChecked()){
                    switchButton.setChecked(false);
                }
            }
        });

My code doesn't work as I aspected, can you help me please, thanks!

Upvotes: 0

Views: 80

Answers (4)

Yashovardhan99
Yashovardhan99

Reputation: 938

As described in this answer here, a single radio button cannot be cleared. Instead you need to clear the whole radio group.

So instead of

radioButton.setChecked(false);

you need to use

((RadioGroup) findViewById(R.id.ID_OF_RADIO_GROUP)).clearCheck();

Also, you haven't specified in your question what exactly isn't working as expected. If this doesn't solve your problem, please comment with what exactly are you getting when you try to trigger the switch or the radio button.

EDIT: Also, you should use setOnCheckedChangeListener() instead of setOnClickListener()

Upvotes: 0

Chintak Patel
Chintak Patel

Reputation: 768

You don't have to use setOnClickListener() but needs to use setnCheckedChangeListener()

See the below code

Switch switchButton = new Switch(this);
RadioButton radioButton = new RadioButton(this);

switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                radioButton.setChecked(false);
            }
        }
    });

radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                switchButton.setChecked(false);
            }
        }
    });

Upvotes: 1

user9591909
user9591909

Reputation:

You have to use the OnCheckedChangeListener() on the switchButton to listen for state changes in the Switch

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

use the same listener on your RadioButton

Upvotes: 1

AndrejH
AndrejH

Reputation: 2119

Do you have the buttons in your view? If you do, you need to use the findViewById function to bind to that view in order to change the buttons. If you just programmatically create a button it wont appear on the view.

RadioButton radioButton = (RadioButton) findViewById(R.id.your_radio_bt_id);

Upvotes: 0

Related Questions