0
votes

Lets suppose I have 2 checkbox ("checkbox1" and "checkbox2") and I set: SetChecked(true) For "checkbox1" and : SetChcked(false) for "checkbox2". my purpose is to relate them and switch between the check state when I click on one of them ,(if "checkbox1" is checked and I checked "checkbox2" then "checkbox2" will be checked and "checkbox1" will be unchecked and by viceversa)
Here is my code :

final CheckBox ChckBoxNo = (CheckBox) promptsView.findViewById(R.id.ChkBoxNo);
final CheckBox ChckBoxYes = (CheckBox) promptsView.findViewById(R.id.ChkBoxYes);
ChckBoxNo.setChecked(true);
ChckBoxYes.setChecked(false);

ChckBoxNo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (ChckBoxNo.isChecked()) {
            ChckBoxYes.setChecked(false);
        } else if (!ChckBoxNo.isChecked()) {
            ChckBoxYes.setChecked(true);
        }

    }

});

ChckBoxYes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (ChckBoxYes.isChecked()) {
            ChckBoxNo.setChecked(false);
        } else if (!ChckBoxYes.isChecked()) {
            ChckBoxNo.setChecked(true);
        }

    }

});

   //these if's allways response to "ChckBoxNo.IsChecked()" ,even if its the 
     // opposite and "ChckBoxYes.isChcked()"          


   if(ChckBoxYes.isChecked())
   {do ...
         }
    else if (ChckBoxNo.isChecked()
      { do ...
               }

When I run the app by visibility evreything is good When I click on "ChckBoxYes" it's checked and "ChckboxNo" is unchecked and viceversa,but its allways get the default value that I set before implenting the "OnCheckedChangeListener" (ChckBoxNo.SetChcked(true), ChckBoxYes.SetChecked(false) , even if its opposite and "ChckBoxYes" is chcked) What should I do in order to fix that? thanks !

1
what you are looking for is a radio button group, not a pair of checkboxesnjzk2
also I think you are confused as to when does your code execute. when do you think your last 6 lines run?njzk2
it is not valid to do that with checkbox?liav bahar
about the last 6 lines I added them to show that its allways respone only to the ChckBoxNo.isChecked()liav bahar
it works, but it is tedious and there is a component that handles that logic for you, so why not use it.njzk2

1 Answers

1
votes

This is not the right way to approach your problem. Luckily there is already a built-in component for that called RadioGroup. You could do something like this:

Have your Activity's xml file something like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radio_button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CheckBox1" />

        <RadioButton
            android:id="@+id/radio_button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CheckBox2" />
    </RadioGroup>
</LinearLayout>

And in your Activity you can interact with this component and handle whatever you want when any of the RadioButtons are tapped like this:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_button1) {
                    //First is checked
                } else if (checkedId == R.id.radio_button2) {
                    // Second is checked
                }
            }
        });
    }
}