@Test public void testGetColor() { int color = ThemeUtil.getColor(getApplicationContext(), R.attr.colorAccentLight); assertNotSame(0, color); color = ThemeUtil.getColor(getApplicationContext(), R.attr.actionBarDivider); assertNotSame(Color.CYAN, color); }
@ColorInt public static int getColor(Context context, int colorAttribute) { final TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(colorAttribute, outValue, true); if (isColor(outValue)) { return outValue.data; } else if (outValue.type == TypedValue.TYPE_REFERENCE || outValue.type == TypedValue.TYPE_STRING) { try { final ColorStateList colors = ContextCompat.getColorStateList(context, outValue.resourceId); return colors.getDefaultColor(); } catch (Resources.NotFoundException ex) { Log.d(ThemeUtil.class.getName(), ex.getMessage()); } } else if (outValue.type == TypedValue.TYPE_NULL) { return getColorFromStyleable(context, styleableForAttr(colorAttribute)); } return Color.CYAN; }
ThemeUtil { @ColorInt public static int getColor(Context context, int colorAttribute) { final TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(colorAttribute, outValue, true); if (isColor(outValue)) { return outValue.data; } else if (outValue.type == TypedValue.TYPE_REFERENCE || outValue.type == TypedValue.TYPE_STRING) { try { final ColorStateList colors = ContextCompat.getColorStateList(context, outValue.resourceId); return colors.getDefaultColor(); } catch (Resources.NotFoundException ex) { Log.d(ThemeUtil.class.getName(), ex.getMessage()); } } else if (outValue.type == TypedValue.TYPE_NULL) { return getColorFromStyleable(context, styleableForAttr(colorAttribute)); } return Color.CYAN; } }
ThemeUtil { @ColorInt public static int getColor(Context context, int colorAttribute) { final TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(colorAttribute, outValue, true); if (isColor(outValue)) { return outValue.data; } else if (outValue.type == TypedValue.TYPE_REFERENCE || outValue.type == TypedValue.TYPE_STRING) { try { final ColorStateList colors = ContextCompat.getColorStateList(context, outValue.resourceId); return colors.getDefaultColor(); } catch (Resources.NotFoundException ex) { Log.d(ThemeUtil.class.getName(), ex.getMessage()); } } else if (outValue.type == TypedValue.TYPE_NULL) { return getColorFromStyleable(context, styleableForAttr(colorAttribute)); } return Color.CYAN; } private  ThemeUtil(); }
ThemeUtil { @ColorInt public static int getColor(Context context, int colorAttribute) { final TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(colorAttribute, outValue, true); if (isColor(outValue)) { return outValue.data; } else if (outValue.type == TypedValue.TYPE_REFERENCE || outValue.type == TypedValue.TYPE_STRING) { try { final ColorStateList colors = ContextCompat.getColorStateList(context, outValue.resourceId); return colors.getDefaultColor(); } catch (Resources.NotFoundException ex) { Log.d(ThemeUtil.class.getName(), ex.getMessage()); } } else if (outValue.type == TypedValue.TYPE_NULL) { return getColorFromStyleable(context, styleableForAttr(colorAttribute)); } return Color.CYAN; } private  ThemeUtil(); static Drawable getDrawable(Context context, final TypedArray typedArray, final int index); static String getString(final TypedArray typedArray, final int index); @ColorInt static int getColor(Context context, int colorAttribute); static int getStyleId(Context context, int themeAttribute); }
ThemeUtil { @ColorInt public static int getColor(Context context, int colorAttribute) { final TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(colorAttribute, outValue, true); if (isColor(outValue)) { return outValue.data; } else if (outValue.type == TypedValue.TYPE_REFERENCE || outValue.type == TypedValue.TYPE_STRING) { try { final ColorStateList colors = ContextCompat.getColorStateList(context, outValue.resourceId); return colors.getDefaultColor(); } catch (Resources.NotFoundException ex) { Log.d(ThemeUtil.class.getName(), ex.getMessage()); } } else if (outValue.type == TypedValue.TYPE_NULL) { return getColorFromStyleable(context, styleableForAttr(colorAttribute)); } return Color.CYAN; } private  ThemeUtil(); static Drawable getDrawable(Context context, final TypedArray typedArray, final int index); static String getString(final TypedArray typedArray, final int index); @ColorInt static int getColor(Context context, int colorAttribute); static int getStyleId(Context context, int themeAttribute);  }