Skip to content

Instantly share code, notes, and snippets.

@alunsford3
Created February 18, 2016 23:40
Show Gist options
  • Save alunsford3/d7a62875e18ca8d0a201 to your computer and use it in GitHub Desktop.
Save alunsford3/d7a62875e18ca8d0a201 to your computer and use it in GitHub Desktop.
Manually resolve color attributes and set for each state
public static int DEFAULT_STATE = -1;
public static int[][] createStatesArray(int... args) {
int length = args.length;
int[][] states = new int[length][];
for (int i = 0; i < length; i++) {
int state = args[i];
if (state == DEFAULT_STATE) {
states[i] = new int[]{};
} else {
states[i] = new int[]{state};
}
}
return states;
}
public static int[] createColorsArray(Context context, int... args) {
int length = args.length;
int[] colors = new int[length];
for (int i = 0; i < length; i++) {
int attr = args[i];
int color = ThemeUtils.getColor(context, attr);
colors[i] = color;
}
return colors;
}
public static int getColor(Context context, int attrResId) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(attrResId, typedValue, true);
return typedValue.data;
}
Context context = getContext();
int[][] states = ThemeUtils.createStatesArray(
android.R.attr.state_selected,
ThemeUtils.DEFAULT_STATE);
int[] colors = ThemeUtils.createColorsArray(
context,
R.attr.greenColor,
R.attr.secondaryTextColor);
ColorStateList colorStateList = new ColorStateList(states, colors);
mTextView.setTextColor(colorStateList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment