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

Changing colors of drawable icons by reusing the single icon with different colors.

A library that manages state using RxJava 2.

CurrencyPicker is a simple library that can be show a currency picker.

Android-CustomToast is a powerful & easy to use Custom Toast library for Android. It runs on API level 14 and upwards.

This library provides some predefined custom toast. Also you can coustomize the toast according to your need

A Single line of code can add a beautiful toast message in your app.

Simple Dribbble client for android.

yandex-translate-api is a simple REST client library for Yandex.Translate. The API provides access to the Yandex online machine translation service. It supports more than 60 languages and can translate separate words or complete texts.

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