ToggleButtonGroup


Source link: https://github.com/nex3z/ToggleButtonGroup

ToggleButtonGroup

A container of toggle buttons, supports multiple / single selection and button customization.

Gradle

dependencies {

  compile 'com.nex3z:toggle-button-group:1.0.3' 
}
 

SingleSelectToggleGroup

You can create a group of single-select toggle buttons with SingleSelectToggleGroup.

<com.nex3z.togglebuttongroup.SingleSelectToggleGroup
  android:id="@+id/group_choices"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:tbgCheckedButton="@+id/choice_a">

<com.nex3z.togglebuttongroup.button.CircularToggle

android:id="@+id/choice_a"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="A"/>

<com.nex3z.togglebuttongroup.button.CircularToggle

android:id="@+id/choice_b"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="B"/>

<!--...-->  </com.nex3z.togglebuttongroup.SingleSelectToggleGroup>

MultiSelectToggleGroup

You can create a group of multi-select toggle buttons with MultiSelectToggleGroup.

<com.nex3z.togglebuttongroup.MultiSelectToggleGroup
  android:id="@+id/group_weekdays"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:tbgChildSpacing="auto">

<com.nex3z.togglebuttongroup.button.CircularToggle

android:id="@+id/sun"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="S"/>

<com.nex3z.togglebuttongroup.button.CircularToggle

android:id="@+id/mon"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="M"/>

<!--...-->  </com.nex3z.togglebuttongroup.MultiSelectToggleGroup>

Flow Buttons to Next Row

<com.nex3z.togglebuttongroup.MultiSelectToggleGroup
  android:id="@+id/group_dummy"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="16dp"
  app:tbgFlow="true"
  app:tbgChildSpacing="auto"
  app:tbgChildSpacingForLastRow="align"
  app:tbgRowSpacing="8dp">

<!--...-->  </com.nex3z.togglebuttongroup.MultiSelectToggleGroup>

With tbgFlow attribute set to true, buttons are allowed to flow to next row when there is no enough space in current row. With tbgButtonSpacing set to auto, buttons are evenly placed in each row.

Listeners

For SingleSelectToggleGroup, use OnCheckedChangeListener to listen to the change of the checked button. Use getCheckedId() to get the id of the checked button.

SingleSelectToggleGroup single = (SingleSelectToggleGroup) findViewById(R.id.group_choices);
 single.setOnCheckedChangeListener(new SingleSelectToggleGroup.OnCheckedChangeListener() {

  @Override
  public void onCheckedChanged(SingleSelectToggleGroup group, int checkedId) {

}
 
}
);

For MultiSelectToggleGroup, use OnCheckedStateChangeListener to be notified of any changes in the group. Use getCheckedIds() to get the ids of all checked buttons.

MultiSelectToggleGroup multi = (MultiSelectToggleGroup) findViewById(R.id.group_weekdays);
 multi.setOnCheckedChangeListener(new MultiSelectToggleGroup.OnCheckedStateChangeListener() {

  @Override
  public void onCheckedStateChanged(MultiSelectToggleGroup group, int checkedId, boolean isChecked) {

}
 
}
);

Custom Button

You can implement custom toggle button in three ways,

CompoundButton implements Checkable interface, toggles itself when being clicked and provides listener for ToggleButtonGroup to keep track of its checked state. CompoundButton is basically a Button / TextView. You can add your custom style and behaviour to the button like CustomCompoundButton from the sample.

If you choose to implement ToggleButton, besides the Checkable interface, you also need to implement a setOnCheckedChangeListener(), which allows ToggleButtonGroup to listen to the changes of any checked states. You need to handle the click event on the button and toggle its checked state properly, as ToggleButtonGroup will not toggle the button when it's being clicked. CustomToggleButton from the sample is a simple toggle button implementing ToggleButton.

CompoundToggleButton implements ToggleButton and toggles its checked state when being clicked. All you need to do is to design the look and feel for the checked and unchecked state. CustomCompoundToggleButton from the sample extends CompoundToggleButton and uses a flipping animation for state transition as shown above.

For more detail, please check the sample.

Resources

WaveProgressView is a simple progress view.

Search View Layout like Lollipop Dialler

Auto resizable EditText.

Android library for swiping to close your layout.

Android podcast app with simplified navigation and lightweight interface.

A beautiful Tinder like card deck & Captain Train like toolbar.

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