dependencies {
implementation 'com.sha.kamel:multi-toggle-button:1.8.3@aar'
}
allprojects {
repositories {
maven { url "https://dl.bintray.com/shabankamel/android" }
}
}
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"
/>
mtb.setOnItemSelectedListener((toggleButton, item, index, label, selected) -> {
toast(selected ? "selected" : "deselected"));
});
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();
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
you can set corners rounded:
mtb.setRoundedCorners();
You can set a default radius of 18dp
:
mtb.setCornerRadius(20);
if you set corner radius with setCornerRadius
, no need to call setRoundedCorners
.
mtb.multipleChoice(true)
you can set the maximum items allowed to be selected
mtb.maxSelectedItems(2, max -> toast("Can't select more than " + max + " items."));
if you call maxSelectedItems
, no need to set multipleChoice(true)
.
You can scroll MultiToggleButton
if the views are out of bounds
mtb.setScrollable(true);
The scroll is disabled by default.
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. |