CheckBoxGroup


Source link: https://github.com/xeoh/CheckBoxGroup

Android CheckBoxGroup

Introduction

CheckBoxGroup provides easy handling of multiple "CheckBox". Don't use switch, if, or multiple listeners for handling CheckBox. Instead, group all CheckBox with CheckBoxGroup.

Requirements

Android API ≥ 2.3 (API Level 9)

Gradle

You can import CheckBoxGroup from jcenter.

repositories {

  jcenter() 
}
  dependencies {

  compile 'com.xeoh.android:checkboxgroup:1.0.1' 
}

Don't: Traditional way to use CheckBoxes as options

We need to deal with every CheckBox when one changes.

public class MainActivity extends AppCompatActivity {

private ArrayList<String> options = new ArrayList<>();

 @Override
protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

CheckBox option1Btn = (CheckBox) findViewById(R.id.option1);

  CheckBox option2Btn = (CheckBox) findViewById(R.id.option2);

  CheckBox option3Btn = (CheckBox) findViewById(R.id.option3);

  CheckBox option4Btn = (CheckBox) findViewById(R.id.option4);

  CheckBox option5Btn = (CheckBox) findViewById(R.id.option5);

options.add("option1");

  options.add("option2");

  options.add("option3");

  options.add("option4");

  options.add("option5");

option1Btn.setOnCheckedChangeListener(onCheckedChangeListener);

  option2Btn.setOnCheckedChangeListener(onCheckedChangeListener);

  option3Btn.setOnCheckedChangeListener(onCheckedChangeListener);

  option4Btn.setOnCheckedChangeListener(onCheckedChangeListener);

  option5Btn.setOnCheckedChangeListener(onCheckedChangeListener);

}

 private CheckBox.OnCheckedChangeListener onCheckedChangeListener

 = new CompoundButton.OnCheckedChangeListener() {

  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

 switch (buttonView.getId()) {

case R.id.option1:

  if (isChecked) options.add("options1");

  else options.remove("options1");

  break;

case R.id.option2:

  if (isChecked) options.add("options2");

  else options.remove("options2");

  break;

case R.id.option3:

  if (isChecked) options.add("options3");

  else options.remove("options3");

  break;

case R.id.option4:

  if (isChecked) options.add("options4");

  else options.remove("options4");

  break;

case R.id.option5:

  if (isChecked) options.add("options5");

  else options.remove("options5");

  break;

 
}

  
}

  Toast.makeText(MainActivity.this,

options.toString(),

Toast.LENGTH_LONG).show();

}
; 
}

Do: Now deal with multiple checkboxes with one class

You can deal with multiple CheckBoxes with CheckBoxGroup. Map your checkBoxes to any value. CheckBoxGroup gives mapped value for all selected checkBoxes.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

HashMap<CheckBox, String> checkBoxMap = new HashMap<>();

  checkBoxMap.put((CheckBox) findViewById(R.id.option1), "Option1");

  checkBoxMap.put((CheckBox) findViewById(R.id.option2), "Option2");

  checkBoxMap.put((CheckBox) findViewById(R.id.option3), "Option3");

  checkBoxMap.put((CheckBox) findViewById(R.id.option4), "Option4");

  checkBoxMap.put((CheckBox) findViewById(R.id.option5), "Option5");

CheckBoxGroup<String> checkBoxGroup = new CheckBoxGroup<>(checkBoxMap,

new CheckBoxGroup.CheckedChangeListener<String>() {

  @Override

  public void onOptionChange(ArrayList<String> options) {

 Toast.makeText(MainActivity.this,

  options.toString(),

  Toast.LENGTH_LONG).show();

  
}

}
);

}

 // Becareful. CheckBoxGroup sets onCheckedChangeListener of checkboxes
// internally. If you need identical listeners for each checkboxes implement
// that algorithm with CheckBox.OnClick 
}

More Usage

// set option1 and 2 checked, 3,4, and 5 unchecked checkBoxGroup.setValues(Arrays.asList("Option1", "Option2"));
  // get entire checkbox values as ArrayList ArrayList<String> selectedValues = checkBoxGroup.getValues();
  // add option6 and make it checked checkBoxGroup.add((CheckBox) findViewById(R.id.option6), "Option6", true);
  // remove option6 checkBoxGroup.add((CheckBox) findViewById(R.id.option6));
  // toggle option5's check value checkBoxGroup.toggleCheckBox("Option5");

See more on Sample Application and JavaDoc

License

CheckBoxGroup is available under the MIT license. See the LICENSE file for more info.

Resources

Utils for setting status bar style in Android App.

This project imitates Tumblr's menu, dragging animations look like a snake.

RecyclerView with zoom effect.

Android Audio encapsulation library, with part Rx support.

A library to avoid boilerplate when testing beans and simple data objects.

"Tired of testing beans (aka: objects with just gettes/setters and no logic)? Do not test them!"

A reactive wrapper around Android's Google Play Services Wear API in 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