Skip to content

Instantly share code, notes, and snippets.

@rbtr
Last active August 29, 2015 14:23
Show Gist options
  • Save rbtr/5e057f4669a4ff5d2ccc to your computer and use it in GitHub Desktop.
Save rbtr/5e057f4669a4ff5d2ccc to your computer and use it in GitHub Desktop.
Accent ColorStateList generator
/*
* The new Design Support Library NavigationView generates a ColorStateList that tints the
* selected item with the theme Primary Color
* This method generates a ColorStateList the same way but with the theme Accent Color
* instead.
*/
/**
* Util method that builds a themed Color State List with the theme Accent Color as the selected
* tint (instead of the Primary Color that NavigationView uses by default)
* @param context used to get the current theme
* @return the ColorStateList
*/
public static ColorStateList themedStateList(Context context) {
TypedValue value = new TypedValue();
// Remove this conditional and set colorAccent to another color if desired
if (context.getTheme().resolveAttribute(R.attr.colorAccent, value, true)) {
int colorAccent = value.data;
if (context.getTheme().resolveAttribute(android.R.attr.textColorPrimary, value, true)) {
ColorStateList baseColor = context.getResources().getColorStateList(value.resourceId);
if (baseColor != null) {
return new ColorStateList(
new int[][]{
{android.R.attr.state_checked},
{} // Null state list sets default colors
},
new int[]{
colorAccent,
baseColor.getDefaultColor(),
}
);
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment