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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
JR Tan
  • 1,703
  • 4
  • 17
  • 35
  • Do you had a look on this ? http://stackoverflow.com/a/27879897 – Sree Sep 10 '15 at 10:50
  • yup, but I have no idea how to access the ColorStateList via code. – JR Tan Sep 10 '15 at 11:10
  • http://stackoverflow.com/a/17788095 – Sree Sep 10 '15 at 11:12
  • it shows how to create but SwitchCompat only can assign ColorStateList to BackgroundTintList & TextColor. – JR Tan Sep 10 '15 at 11:19
  • Seems that you cannot modify themes / styles programmatically: http://stackoverflow.com/questions/12357768/modify-existing-theme http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view . 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. – headuck Sep 16 '15 at 04:35
  • 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. – JR Tan Sep 16 '15 at 11:56

3 Answers3

63

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));
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
dtx12
  • 4,438
  • 1
  • 16
  • 12
1
    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
            }));
  • 1
    Welcome to Stack Overflow. Please explain what you are doing along with the code, so others could also be benefitted. – Tushar Aug 18 '20 at 09:06
-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);
            }}
Kerelos
  • 83
  • 4