EnhancedCheckBox

Additional

Language
Java
Version
N/A
Created
Sep 26, 2014
Updated
May 30, 2019
Owner
Kiran Rao (curioustechizen)
Activity
N/A
Badge
Generate
Download
Source code

Advertisement

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 on user action; not when you set it programmatically (e.g. on HTTP GET response).

Java file (1 KB): EnhancedCheckBox.java

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

Java file (913 bytes): EnhancedSwitch.java

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

Java file (888 bytes): EnhancedToggleButton.java

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

Java file (280 bytes): ProgrammaticallyCheckable.java

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

Java file (163 bytes): Usage.java

boolean initialSwitchState = getInitialSwitchState();
mSwitch.setCheckedProgrammatically(initialSwitchState); //This does not trigger the OnCheckedChangedListener