1

How would I make a checkbox uncheck a previously checked checkbox and vice versa. I am trying to develop an app where checking you can only choose one of the two options. I could not seem to get onCheckedChangeListener working. I don't want radio buttons because they can't be unclicked.

I tried this code posted by Noob under a similar question. I need to uncheck a checkbox when a particular checkbox is checked

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setalarm);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    final CheckBox chk1 = (CheckBox) findViewById(R.id.checkBox1);
    final CheckBox chk2 = (CheckBox) findViewById(R.id.checkBox2);

    chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            chk2.setChecked(false);
        }
    });
    chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            chk1.setChecked(false);
        }
    });
}
Community
  • 1
  • 1
Chris Chen
  • 25
  • 5
  • What have you tried to do to get `onCheckedChangeListener` working? Can you post some code? – zero298 Jan 21 '15 at 23:04
  • I added a link to the code I tried. – Chris Chen Jan 21 '15 at 23:11
  • And what's the problem with the code you're trying? Do you see an error? Is the event not firing at all? Paste your code into the question for us to try and help you better. – Leigh Jan 21 '15 at 23:12

1 Answers1

0

If you're definitely sure you don't want to use RadioButtons and only have two possible CheckBox options something simple like this should do the trick:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_name);

    final CheckBox c1 = (CheckBox) findViewById(R.id.c1);
    final CheckBox c2 = (CheckBox) findViewById(R.id.c2);

    c1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            c2.setChecked(false);
            c1.setChecked(b);
        }
    });
    c2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            c1.setChecked(false);
            c2.setChecked(b);
        }
    });
}
Leigh
  • 1,495
  • 2
  • 20
  • 42
  • Quick question, I'm new to coding, what do you mean by implement logic? – Chris Chen Jan 21 '15 at 23:15
  • The value of `b` inside the `onCheckChanged` tells you if the `CheckBox` is checked on or not. Maybe you want to do something when the value of one of those `CheckBoxes` is changed? Like show a Dialog. This is what I mean by "Implement Logic". – Leigh Jan 21 '15 at 23:18
  • Could you give an example of a code I could fill it in with, like a textview. Or can I have it do nothing? Also, everything looks good except the c2.setChecked(false), the c2 is underlined red likewise for the c1 in c1.setChecked(false), is this because I haven't implemented the value of b? – Chris Chen Jan 21 '15 at 23:20
  • You don't have to do anything with the `if (b)` statement, you can remove it if you don't need to perform any action when either `CheckBox` is checked / unchecked. Regarding the red squiggles; this is because you are accessing the value of `c1` and `c2` from within an inner class you should declare them as [`final`](http://stackoverflow.com/questions/15655012/how-final-keyword-works). I have updated my answer appropriately. – Leigh Jan 21 '15 at 23:27
  • Thanks! I just tried running the application, however whenever I try to click a checkbox, the application crashes! I updated my post with the code you gave me which I tried inputting. Maybe you know why it is crashing? – Chris Chen Jan 21 '15 at 23:34
  • @ChrisChen Your code looks fine to me, can you add the stack trace error to your question? – Leigh Jan 21 '15 at 23:37
  • I'm not quite sure where to find that however the one error I saw was: ConcurrentModificationException: null – Chris Chen Jan 21 '15 at 23:40
  • You can find the stack trace in the Logcat tab under the Debug panel in Android Studio. `ConcurrentModificationException` sounds like your removing items from a `List` of some kind? Do you have other code in your app that may be causing this error? – Leigh Jan 21 '15 at 23:43
  • I don't think so as it was working fine before, but I think this is the stack trace! Im not sure how to post it but here is a part: java.lang.IllegalStateException: Could not find a method chk1(View) in the activity class testone.application.alarm.myapplication.setalarm for onClick handler on view class android.widget.CheckBox with id 'checkBox1' – Chris Chen Jan 21 '15 at 23:45
  • Do you have an `android:onClick` attribute applied to your `CheckBox` inside your layout (activity_setalarm.xml)? If so you should remove it. – Leigh Jan 21 '15 at 23:57
  • Yes, I did! I deleted it and now it works! Thank you so much! – Chris Chen Jan 22 '15 at 00:00
  • @ChrisChen You're welcome. Note I have also updated my post and added `c1.setChecked(b);` to ensure the correct `CheckBox` is checked when clicking each of them. – Leigh Jan 22 '15 at 00:02
  • I actually have a follow up question, which is, how would I make the other option that was not checked, fade but still be clickable if the user changes his/her mind? – Chris Chen Jan 22 '15 at 00:22
  • @ChrisChen You should probably start a new question if you're stuck with fading out a `CheckBox` I would however suggest you use something like [this](http://stackoverflow.com/a/10498226/513450) in the `onCheckedChanged` event. – Leigh Jan 22 '15 at 11:12
  • It doesn't seem to recognize it when I replace 'b' with it. This is my new question page:http://stackoverflow.com/questions/28102745/how-to-make-an-option-fade-but-still-be-clickable-on-android-studio/28102805#28102805 – Chris Chen Jan 23 '15 at 04:21