Closed renaldoaluska closed 7 years ago
this is the example: i want to get the selected one like "A" or "B" or "C" or "D" not the position, how to do it?
btw im sorry because i accidentally press the close issue button.
Hi alfaaluska, ToggleButtonGroup
doesn't have a method to get the button text by position. I'll try to add a convenient method in the next release.
For now you can get the button text from the data source:
final SingleSelectToggleGroup singleSelect =
(SingleSelectToggleGroup) findViewById(R.id.single_selection_group);
final String[] choices = getResources().getStringArray(R.array.choices);
final List<String> choicesList = new ArrayList<>(Arrays.asList(choices));
singleSelect.setButtons(choicesList);
singleSelect.setOnCheckedChangeListener(new ToggleButtonGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChange(int position, boolean isChecked) {
String selectedText = choicesList.get(position);
}
});
If you have the data source (choicesList
) and the selected position (position
), then you can get the text on the selected button from data source (choicesList.get(position))
.
@nex3z Thank you so much! It perfectly works, but the default selected text will be declared as null
if I don't touch/click it first.
Remove the default selected button maybe can fix this problem, how can I do that? So, users should click the button first, or maybe another solutions?
Thanks before!
For SingleSelectToggleGroup
, the default selected button will be the first button in the group, so you can assume that the default selected text is the text on the first button.
private String mSelectedText;
private void setupSingleSelectToggleGroup() {
final SingleSelectToggleGroup singleSelect =
(SingleSelectToggleGroup) findViewById(R.id.single_selection_group);
final String[] choices = getResources().getStringArray(R.array.choices);
final List<String> choicesList = new ArrayList<>(Arrays.asList(choices));
singleSelect.setButtons(choicesList);
// The defult selected button is the first button in the group
mSelectedText = choicesList.get(0);
singleSelect.setOnCheckedChangeListener(new ToggleButtonGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChange(int position, boolean isChecked) {
mSelectedText = choicesList.get(position);
}
});
}
You can also call setCheckedAt()
on SingleSelectToggleGroup
instance (singleSelect
) to set other button as default selection, just remember to update the local storage of the selected text (mSelectedText
) accordingly.
singleSelect.setCheckedAt(2, true);
mSelectedText = choicesList.get(2);
For the multi-select scenario, as user can select an arbitrary number of buttons, so MultiSelectToggleGroup
does not assume any default selection. I think it's OK to initialize the selected texts to be null or an empty collection. If you need any buttons to be selected by default, you can usesetCheckedPositions()
or setCheckedAt()
on MultiSelectToggleGroup
.
Thanks a lot for your explanation, it works well 👍
how do i define the selected text on string like:
SingleSelectToggleGroup nominal = (SingleSelectToggleGroup) findViewById(R.id.nominal);
i've tried
String choosennominal = nominal.getCheckedPositions().toString();
andString choosennominal = nominal.getTextButtons().toString();
but i still don't get the button text, help me please