Last active
September 10, 2018 10:56
-
-
Save PPartisan/0f16c5e32de05af22d4e2a141b4ea644 to your computer and use it in GitHub Desktop.
Utility methods that create a ColorStateList for use with SwitchCompat. Allows for dynamic colour changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final int SWITCH_STATE_LIST_ALPHA = (int)(0.3f*255); | |
public static ColorStateList buildSwitchCompatColorStateListFromResId( | |
Context context, int activatedColorResId) { | |
return buildSwitchCompatColorStateList( | |
context, ContextCompat.getColor(context, activatedColorResId) | |
); | |
} | |
public static ColorStateList buildSwitchCompatColorStateList(Context context, int activatedColor) { | |
final int disabledColor = ContextCompat.getColor(context, R.color.grey_300); | |
final int disabledColorAlpha = Color.argb( | |
SWITCH_STATE_LIST_ALPHA, | |
Color.red(disabledColor), | |
Color.green(disabledColor), | |
Color.blue(disabledColor) | |
); | |
final int[][] states = new int[][] { | |
new int[] { -android.R.attr.state_enabled }, | |
new int[] { android.R.attr.state_checked }, | |
new int[0] | |
}; | |
final int[] colors = new int[] { disabledColorAlpha, activatedColor, disabledColor }; | |
return new ColorStateList(states, colors); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment