1

I currently have a two checkboxes, where when one is clicked, the other automatically unchecks. I would like to keep that however have the unchecked one become a faded white color, yet still be clickable and readable if the user decides to change his/her mind.

This is the current code I have:

 chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (CheckBox.isChecked(chk1)) {
            chk2.setChecked(false);
            chk1.setChecked(b);
            chk2.setAlpha(0.5f);
        }
    });

    chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (CheckBox.isChecked(chk2)) {
            chk1.setChecked(false);
            chk2.setChecked(b);
            chk1.setAlpha(0.5f);
        }
    });
Chris Chen
  • 25
  • 5

2 Answers2

1

chk2.setAlpha(0.5f) would make it appear faded.

chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            chk2.setChecked(false);
            chk1.setChecked(b);
            chk2.setAlpha(0.5f);
            chk1.setAlpha(1f);
        }
    });

chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        chk1.setChecked(false);
        chk2.setChecked(b);
        chk1.setAlpha(0.5f);
        chk2.setAlpha(1f);
    }
});
Mark Hetherington
  • 1,612
  • 14
  • 24
  • I've updated my code in the opening post, it works however once faded, it doesn't come back. So, if I check box1, box2 will fade, however if I click box2, box1 does fade, BUT it box2 is still faded although clicked! – Chris Chen Jan 23 '15 at 05:31
  • Actually, I also was wondering something else. If chk1 is checked, chk2 will be faded, however if I uncheck chk1, chk2 stays faded, is there a way to fix this? – Chris Chen Jan 24 '15 at 02:08
  • @ChrisChen this is where `boolean b` comes in handy, you can write an `if` statement inside your `onCheckedChanged` and check if `chk1` is unchecked, if so you can reset the alpha to `1f`. – Leigh Jan 24 '15 at 22:41
  • I edited the opening post and added if (CheckBox.isChecked(chk1)) {, however, I get a red squigly under isChecked saying that "Non-Static method 'isChecked' cannot be referenced from a static context." – Chris Chen Jan 24 '15 at 23:17
  • chk1.isChecked() the method is not static (you should lookup what this means it will help). http://developer.android.com/reference/android/widget/CheckBox.html – Mark Hetherington Jan 25 '15 at 00:21
  • I looked through that, but I'm not sure what I'm looking for. I've tried to do it a different way: if chk1.isChecked(true), however "true" isn;t allowed in boolean – Chris Chen Jan 25 '15 at 03:01
  • chk1.isChecked(true) returns a boolean of true or false telling you if the checkbox chk1 is checked. – Mark Hetherington Jan 26 '15 at 05:20
0

You can use different icons for checkboxes when they are in selected and unselected state. Also change the color of the text to make it look faded.

To help with changing checkbox icons, this is helpful

Change icons of checked and unchecked for Checkbox for Android

You can use to change the checkbox icon and text color, chk2.setTextColor(getResources().getColor(R.color.gray)); chk2.setButtonDrawable(R.drawable.unchecked);

Community
  • 1
  • 1
Divya Y
  • 183
  • 1
  • 9
  • How would this work if I only want it to change when the other checkbox is clicked? And for it to change back when it is checked. – Chris Chen Jan 23 '15 at 04:02
  • Hi Chris, If I got the problem right, when you click on chkbox 1, you want to fade out and uncheck the chkbox 2 ? The above lines will help you with same. – Divya Y Jan 23 '15 at 04:32
  • Yes, is there a way to add it the fade, as I already have the uncheck part. I've tried inserting that for b however I come up with an error. – Chris Chen Jan 23 '15 at 04:36