/android-multitoggle

A simple multi-state toggle button for Android.

Primary LanguageJava

MultiToggleButton

A simple Android multi toggle button

alt text

Installation

Download

dependencies {
    implementation 'com.sha.kamel:multi-toggle-button:1.8.3@aar'
}

allprojects {
 repositories { 
  maven { url "https://dl.bintray.com/shabankamel/android" } 
 }
}

Usage

Add a button to layout:

<com.sha.kamel.multitogglebutton.MultiToggleButton  
  android:id="@+id/mtb"  
  android:layout_width="match_parent"  
  android:layout_height="wrap_content"  
  android:layout_marginTop="10dip"  
  mtb:labels="@array/dogs_array"  
  mtb:mtbScrollable="true"  
  mtb:mtbRoundedCorners="true"  
  mtb:mtbPressedColor="@color/orange_pressed"  
  mtb:mtbUnpressedColor="@color/orange"  
  mtb:mtbColorPressedText="@color/white"  
  mtb:mtbColorUnpressedText="@color/white"  
  mtb:mtbCornerRadius="8dp"  
  />

Listen to item selection

 mtb.setOnItemSelectedListener((toggleButton, item, index, label, selected) -> {  
  toast(selected ? "selected" : "deselected"));  
});

Get selected items

You can get selected items.

Selected selected = toggleButton.getSelected();

boolean isAnySelected = selected.isAnySelected();
boolean isAllSelected = selected.isAllSelected();
boolean isSingleItem = selected.isSingleItem();

int singleItemPosition = selected.getSingleItemPosition();
TextView singleItem = selected.getSingleItem();

List<TextView> selectedItems = selected.getSelectedItems();
List<Integer> selectedPositions = selected.getSelectedPositions();

Colors

You can select any desired color for different states.

mtb.setColorRes(R.color.mtb_green, R.color.mtb_gray);

There're many methods to set colors. Take a look at ToggleButton

Rounded corners

you can set corners rounded:

mtb.setRoundedCorners();

Corners Radius

You can set a default radius of 18dp :

mtb.setCornerRadius(20);
Note:

if you set corner radius with setCornerRadius, no need to call setRoundedCorners.

Multiple Choice

mtb.multipleChoice(true)

Max items to select

you can set the maximum items allowed to be selected

mtb.maxSelectedItems(2, max -> toast("Can't select more than " + max + " items."));

Note

if you call maxSelectedItems, no need to set multipleChoice(true).

Scroll

You can scroll MultiToggleButton if the views are out of bounds

mtb.setScrollable(true);

Note

The scroll is disabled by default.

Attributes:

Attribute name Description
labels Labels of each items in button
mtbPressedColor Color of pressed button
mtbUnpressedColor Color of unpressed button
mtbColorPressedText Color of text for pressed button
mtbColorUnpressedText Color of text for un pressed button
mtbCornerRadius Corner radius
mtbRoundedCorners If true, the corners will be rounded. If corner radius is not set a default radius 18 will be set.
mtbMultipleChoice multiple items choice. The default is false.
mtbScrollable If true, items will be scrollable if it's out of screen bounds. The default is false
mtbSelectFirstItem If true, first item will be selected. The default is true.
mtbTextAllCaps All text caps.The default is true.

See 'app' module for the full code.

License

Apache license 2.0