Issue
Can I programmatically check a checkbox
and make it call whatever it is coded to do once checked or unchecked?
For example, if I have a checkbox like this
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
Toast.makeText(getContext(), "You checked the checkbox!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getContext(), "You unchecked the checkbox!", Toast.LENGTH_SHORT).show();
}
}
});
And then would've called
checkBox.setChecked(true);
my checkbox would appear checked but it wouldnt make the toast.
Solution
You can do any of the following
1. Create the listener object separate and call it manually when you call setChecked
2. Extract the method for implementation of onCheckedChanged
and call it manually on your change.
Answered By - Yousef khan
Answer Checked By - Gilberto Lyons (JavaFixing Admin)