Skip to content

Instantly share code, notes, and snippets.

@curioustechizen
Last active September 14, 2020 05:50
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save curioustechizen/81d793f4b14bb3f649d1 to your computer and use it in GitHub Desktop.
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…
/**
* 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);
}
}
@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);
}
}
public class EnhancedToggleButton extends ToggleButton implements ProgrammaticallyCheckable {
private CompoundButton.OnCheckedChangeListener mListener = null;
public EnhancedToggleButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public EnhancedToggleButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EnhancedToggleButton(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);
}
}
/**
* 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);
}
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