nex3z / ToggleButtonGroup

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

Change LabelToggle background and border color for checked/unchecked state programmatically? #40

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hi @nex3z , Thanks for developing such a great library. I am using your library in my project, I would like to know how I can simply just use my own background and border color and everything else the same. Currently i am doing something like this:

private void initMultiSelectToggleGroup() {
        MultiSelectToggleGroup interestsMultiSelectToggleGroup = findViewById(R.id.interests_multi_select_toggle_group);
        String[] interests = {"Photography", "Hiking", "Skiing"};
        for (String interest : interests) {
            LabelToggle toggle = new LabelToggle(this);
            toggle.setText(interest);
            toggle.setMarkerColor(R.color.colorPrimaryLight);
            interestsMultiSelectToggleGroup.addView(toggle);
        }
    }

But with this I am getting a greyish border and background color instead of my defined colorPrimaryLight.

PS: You mentioned that we can use our custom buttons, but just for changing the background and border color, I don't think creating a whole new custom button is a good idea, plus I (beginner) am confused on how I can create a custom button (Complete Steps to create a custom button) . Please help me with this

Thanks in advance.

nex3z commented 5 years ago

When calling setMarkerColor(), pass a color value instead of a color resource id (e.g. R.color.colorPrimaryLight).

You can get color value from color resource id by ContextCompat.getColor().

For examlpe,

MultiSelectToggleGroup interestsMultiSelectToggleGroup = findViewById(R.id.interests_multi_select_toggle_group);
String[] interests = {"Photography", "Hiking", "Skiing"};
for (String interest : interests) {
    LabelToggle toggle = new LabelToggle(this);
    toggle.setText(interest);
    toggle.setMarkerColor(ContextCompat.getColor(this, R.color.colorPrimaryLight)); // get actual color value from resource id
    interestsMultiSelectToggleGroup.addView(toggle);
}

will give you device-2018-10-06-214513

ghost commented 5 years ago

Thanks a lot Man, works perfect. The owners of libraries usually don't help this much like you do. Can I have one more question here, or should I create a separate issue?

nex3z commented 5 years ago

Of course. Please create a separate issue.