Last active
September 14, 2020 05:50
-
-
Save curioustechizen/81d793f4b14bb3f649d1 to your computer and use it in GitHub Desktop.
Android programmatically checkable widgets, differentiate between user clicks & programmatic setChecked calls. Based on this answer on StackOverflow: http://stackoverflow.com/a/14307643/570930. There are times when setting a switch to ON results in some action (Save to DB/ perform an HTTP POST etc). However, you want this action to happen only o…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An enhanced {@code CheckBox} that differentiates between user clicks and | |
* programmatic clicks. In particular, the {@code OnCheckedChangeListener} is | |
* <strong>not</strong> triggered when the state of the checkbox is changed | |
* programmatically. | |
* | |
*/ | |
public class EnhancedCheckBox extends CheckBox implements ProgrammaticallyCheckable{ | |
private CompoundButton.OnCheckedChangeListener mListener = null; | |
public EnhancedCheckBox(Context context) { | |
super(context); | |
} | |
public EnhancedCheckBox(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
public EnhancedCheckBox(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
public void setOnCheckedChangeListener( | |
CompoundButton.OnCheckedChangeListener listener) { | |
if (this.mListener == null) {this.mListener = listener;} | |
super.setOnCheckedChangeListener(listener); | |
} | |
/** | |
* Set the checked state of the checkbox programmatically. This is to differentiate it from a user click | |
* @param checked Whether to check the checkbox | |
*/ | |
@Override | |
public void setCheckedProgrammatically(boolean checked) { | |
super.setOnCheckedChangeListener(null); | |
super.setChecked(checked); | |
super.setOnCheckedChangeListener(mListener); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) | |
public class EnhancedSwitch extends Switch implements ProgrammaticallyCheckable { | |
private CompoundButton.OnCheckedChangeListener mListener = null; | |
public EnhancedSwitch(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
public EnhancedSwitch(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public EnhancedSwitch(Context context) { | |
super(context); | |
} | |
@Override | |
public void setOnCheckedChangeListener( | |
CompoundButton.OnCheckedChangeListener listener){ | |
if(this.mListener == null) {this.mListener = listener;} | |
super.setOnCheckedChangeListener(listener); | |
} | |
@Override | |
public void setCheckedProgrammatically(boolean checked){ | |
super.setOnCheckedChangeListener(null); | |
super.setChecked(checked); | |
super.setOnCheckedChangeListener(mListener); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Interface for UI widgets (particularly, checkable widgets) whose checked state can be changed programmatically, without triggering the {@code OnCheckedChangeListener} | |
* | |
*/ | |
public interface ProgrammaticallyCheckable { | |
void setCheckedProgrammatically(boolean checked); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boolean initialSwitchState = getInitialSwitchState(); | |
mSwitch.setCheckedProgrammatically(initialSwitchState); //This does not trigger the OnCheckedChangedListener |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment