EnhancedCheckBox


Source link: https://gist.github.com/curioustechizen/81d793f4b14bb3f649d1

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

Resources

Tab Layout for ViewPager just like Weixin.

This lib produces some sugar and type safety for Android Shared Preferences.

Reactive layer (similar to the pattern repository) for the Realm. Main idea is using reactive layer for access to the Realm. If you make a request to the layer always return Observable. The layer will remember the entity, the predicate and returns Observable in the future if the entity will change the subject learns about it.

SQLDelight generates Java models from your SQL CREATE TABLE statements. These models give you a typesafe API to read & write the rows of your tables. It helps you to keep your SQL statements together, organized, and easy to access from Java.

Phonematter provides a simple to use class for formatting and validating phone numbers in Android apps.

Android SharedPreference delegation for Kotlin.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes