Add methods for creating StateListDrawable and ColorStateList for checked state

This commit is contained in:
Alex Sytnyk 2018-04-05 15:58:01 +03:00
parent 9789674c90
commit ae6ef269b1

View file

@ -142,6 +142,13 @@ public class AndroidUtils {
return null;
}
public static ColorStateList createCheckedColorStateList(Context ctx, boolean night,
@ColorRes int lightNormal, @ColorRes int lightChecked,
@ColorRes int darkNormal, @ColorRes int darkChecked) {
return createColorStateList(ctx, night, android.R.attr.state_checked,
lightNormal, lightChecked, darkNormal, darkChecked);
}
public static ColorStateList createPressedColorStateList(Context ctx, @ColorRes int normal, @ColorRes int pressed) {
return createPressedColorStateList(ctx, false, normal, pressed, 0, 0);
}
@ -149,21 +156,36 @@ public class AndroidUtils {
public static ColorStateList createPressedColorStateList(Context ctx, boolean night,
@ColorRes int lightNormal, @ColorRes int lightPressed,
@ColorRes int darkNormal, @ColorRes int darkPressed) {
return createColorStateList(ctx, night, android.R.attr.state_pressed,
lightNormal, lightPressed, darkNormal, darkPressed);
}
private static ColorStateList createColorStateList(Context ctx, boolean night, int state,
@ColorRes int lightNormal, @ColorRes int lightState,
@ColorRes int darkNormal, @ColorRes int darkState) {
return new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed},
new int[]{state},
new int[]{}
},
new int[]{
ContextCompat.getColor(ctx, night ? darkPressed : lightPressed),
ContextCompat.getColor(ctx, night ? darkState : lightState),
ContextCompat.getColor(ctx, night ? darkNormal : lightNormal)
}
);
}
public static StateListDrawable createCheckedStateListDrawable(Drawable normal, Drawable checked) {
return createStateListDrawable(normal, checked, android.R.attr.state_checked);
}
public static StateListDrawable createPressedStateListDrawable(Drawable normal, Drawable pressed) {
return createStateListDrawable(normal, pressed, android.R.attr.state_pressed);
}
private static StateListDrawable createStateListDrawable(Drawable normal, Drawable stateDrawable, int state) {
StateListDrawable res = new StateListDrawable();
res.addState(new int[]{android.R.attr.state_pressed}, pressed);
res.addState(new int[]{state}, stateDrawable);
res.addState(new int[]{}, normal);
return res;
}