Get color value programmatically when it is referenced (topic)

Consider this:

styles.xml

<style name="BlueTheme" parent="@android:style/Theme.Black.NoTitleBar"> <item name="theme_color">@color/theme_color_blue</item> </style> 

attrs.xml

 <attr name="theme_color" format="reference" /> 

color.xml

 <color name="theme_color_blue">#ff0071d3</color> 

The topic icon refers to the topic. How can I get theme_color (reference) programmatically? Normally I would use getResources().getColor() , but not in this case, because it referenced!

+74
android android-resources android-theme
Jun 24 '13 at 14:12
source share
5 answers

This should complete the task:

 TypedValue typedValue = new TypedValue(); Theme theme = context.getTheme(); theme.resolveAttribute(R.attr.theme_color, typedValue, true); @ColorInt int color = typedValue.data; 

Also remember to apply the theme to your activity before calling this code. Or use:

 android:theme="@style/Theme.BlueTheme" 

in your manifest or call (before calling setContentView(int) ):

 setTheme(R.style.Theme_BlueTheme) 

in onCreate() .

I tested it with your values ​​and it worked perfectly.

+174
Jun 24 '13 at 14:16
source share

This worked for me:

 int[] attrs = {R.attr.my_attribute}; TypedArray ta = context.obtainStyledAttributes(attrs); int color = ta.getResourceId(0, android.R.color.black); ta.recycle(); 

if you want to get the sixth line from it:

 Integer.toHexString(color) 
+16
07 Sep '16 at 18:57
source share

To add to the accepted answer if you are using kotlin.

 fun Context.getColorFromAttr( @AttrRes attrColor: Int, typedValue: TypedValue = TypedValue(), resolveRefs: Boolean = true ): Int { theme.resolveAttribute(attrColor, typedValue, resolveRefs) return typedValue.data } 

and then in your work you can do

textView.setTextColor(getColorFromAttr(R.attr.color))

+12
Jul 27 '18 at 15:55
source share

If you want to get some colors, you can use:

 int[] attrs = {R.attr.customAttr, android.R.attr.textColorSecondary, android.R.attr.textColorPrimaryInverse}; Resources.Theme theme = context.getTheme(); TypedArray ta = theme.obtainStyledAttributes(attrs); int[] colors = new int[attrs.length]; for (int i = 0; i < attrs.length; i++) { colors[i] = ta.getColor(i, 0); } ta.recycle(); 
+2
Jan 07 '18 at 15:16
source share

Here's a short Java utility method that takes multiple attributes and returns an array of integers. :)

 /** * @param context Pass the activity context, not the application context * @param attrFields The attribute references to be resolved * @return int array of color values */ @ColorInt static int[] getColorsFromAttrs(Context context, @AttrRes int... attrFields) { int length = attrFields.length; Resources.Theme theme = context.getTheme(); TypedValue typedValue = new TypedValue(); @ColorInt int[] colorValues = new int[length]; for (int i = 0; i < length; ++i) { @AttrRes int attr = attrFields[i]; theme.resolveAttribute(attr, typedValue, true); colorValues[i] = typedValue.data; } return colorValues; } 
0
May 05 '19 at 15:18
source share



All Articles