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

Method setbuttons don't exist #16

Closed IgordeOliveira closed 6 years ago

IgordeOliveira commented 6 years ago

I need to get the text from the SingleSelectToggleGroup and was following this issue closed https://github.com/nex3z/ToggleButtonGroup/issues/3 but the setbuttons method does not exist, is there any other way to get the text? because from what I saw, the id always changes.

nex3z commented 6 years ago

There was a major update in the 1.0.0 release on 25 Apr. setbuttons() has been removed since SingleSelectToggleGroup or MultiSelectToggleGroup does not provide any default button implementation in itself, so that developers can design their own buttons to be used in toggle group.

To add buttons to the toggle group, you can declare your buttons in the XML layout, or build and add the buttons programmatically, just like adding views to any other ViewGroup.

Similar approach can be used to retrieve the text on the button:

final SingleSelectToggleGroup single = 
        (SingleSelectToggleGroup) findViewById(R.id.group_choices);

final String[] choices = getResources().getStringArray(R.array.choices);
for (String choice: choices) {
    CircularToggle toggle = new CircularToggle(this);
    toggle.setText(choice);
    single.addView(toggle);
}

single.setOnCheckedChangeListener(new SingleSelectToggleGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(SingleSelectToggleGroup group, int checkedId) {
        CircularToggle toggle = (CircularToggle) group.findViewById(checkedId);
        // Retrieve the text on the selected button
        mSelectedText = toggle.getText().toString();
        Log.v(LOG_TAG, "onCheckedChanged(): checkedId = " + checkedId
                + ", mSelectedText = " + mSelectedText);
    }
});

// Note SingleSelectToggleGroup does not assume any default selection now.
// You have to specify the default selection explicitly.
((CircularToggle) single.getChildAt(0)).setChecked(true);

The ID on the button might change, especially when the button does not have an id attribute in the XML layout or the button is built and added programmatically, in both cases the ID of the button is generated by the toggle group at run time. However the ID is generated, you can always find the right button by its ID.

IgordeOliveira commented 6 years ago

Works, thanks