Change colorControlActivated color programmatically
Asked Answered
A

3

16

I have read a few threads regarding the color, but all of them has to set via style.xml.

For now I'm using this to determine the color.

<style name="Color1SwitchStyle">
    <item name="colorControlActivated">#0e8488</item>
</style>'

Is it possible to change the color of a SwitchCompat/Checkbox without using XML, for instance using code?

Augmentation answered 10/9, 2015 at 10:43 Comment(6)
Do you had a look on this ? stackoverflow.com/a/27879897Gelsenkirchen
yup, but I have no idea how to access the ColorStateList via code.Augmentation
stackoverflow.com/a/17788095Gelsenkirchen
it shows how to create but SwitchCompat only can assign ColorStateList to BackgroundTintList & TextColor.Augmentation
Seems that you cannot modify themes / styles programmatically: https://mcmap.net/q/747313/-modify-existing-theme #2016749 . You may consider using a custom drawable for say the thumb of a switchcompat by using setThumbResource(), in which case the Tint manager will not tint it and you can apply color to it programmatically.Mckinney
For now I'm using third party library which included the change color configuration, however I'm looking for more official way as 3rd party library won't get frequent updates.Augmentation
A
64

Actually, it's not hard to do.

Example:

int[][] states = new int[][] {
        new int[] {-android.R.attr.state_checked},
        new int[] {android.R.attr.state_checked},
};

int[] thumbColors = new int[] {
        Color.BLACK,
        Color.RED,
};

int[] trackColors = new int[] {
        Color.GREEN,
        Color.BLUE,
};

SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switchControl);
AppCompatCheckBox checkBox = (AppCompatCheckBox) findViewById(R.id.checkbox);
checkBox.setSupportButtonTintList(new ColorStateList(states, thumbColors));
DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getThumbDrawable()), new ColorStateList(states, thumbColors));
DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getTrackDrawable()), new ColorStateList(states, trackColors));
Aerospace answered 16/9, 2015 at 18:11 Comment(2)
Works like a charm!Biome
that is absolutely perfect and clear answerBrewery
T
2
    DrawableCompat.setTintList(switch.getThumbDrawable(), new ColorStateList(
            new int[][]{
                    new int[]{android.R.attr.state_checked},
                    new int[]{}
            },
            new int[]{
                    Color.parseColor("Write Color code-for ex #ffffffff"),
                    Color.GRAY
            }));
Trifolium answered 18/8, 2020 at 7:52 Comment(1)
Welcome to Stack Overflow. Please explain what you are doing along with the code, so others could also be benefitted.Pemberton
F
-1

Another way to do, change the background color:

setBackgroundColor(android.graphics.Color.GREEN);

As:

holper.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked){

                buttonView.setBackgroundColor(android.graphics.Color.GREEN);
            }}
Felspar answered 16/8, 2019 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.