nex3z / ToggleButtonGroup

A group of flowable toggle buttons, with multiple / single selection support and button customization.
Apache License 2.0
421 stars 48 forks source link

get position of selected #20

Closed IgordeOliveira closed 6 years ago

IgordeOliveira commented 6 years ago

I have a MultiSelectToggleGroup with all the days of the week as in the image, I need to check these days (if they beat the current day), but not to use the texts because it has repeated (SATURDAY and SUNDAY is "S"), how do I get the position of items checked? or another way to do it?

image

nex3z commented 6 years ago

You can use T findViewById (int id) and int indexOfChild (View child) to find the position of a button:

final MultiSelectToggleGroup multi = (MultiSelectToggleGroup) findViewById(R.id.group_weekdays);

Set<Integer> checkedIds = multi.getCheckedIds();
Set<Integer> positions = new HashSet<>(); // Holder for all checked positions
for (int id : checkedIds) {
    View view = multi.findViewById(id);
    int position = multi.indexOfChild(view);
    positions.add(position);
}

You can also add extra data like position on each button using tags, so that you can get the position from the button directly.

IgordeOliveira commented 6 years ago

Works, thanks