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); } }); 

2 Answers 2

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); } }); 
Sign up to request clarification or add additional context in comments.

7 Comments

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!
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?
@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.
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."
chk1.isChecked() the method is not static (you should lookup what this means it will help). developer.android.com/reference/android/widget/CheckBox.html
|
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);

3 Comments

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.
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.