2

I need to get some default theme color values programmatically (e.g. windowBackground, colorPrimary). I'm executing the code from an Activity. My target android API is 21. I'm using a Theme.Material theme. I've tried:

var attributeValue = new Android.Util.TypedValue(); this.Theme.ResolveAttribute(Resource.Attribute.colorPrimary, attributeValue, true) 

with different resource identifier, but i always get a Android.Util.DataType.Null value.

1
  • did you check my answer ?? Commented Nov 24, 2016 at 12:46

1 Answer 1

1

Use this code I have tested

For WindowBackground :

Code :

Android.Util.TypedValue a = new Android.Util.TypedValue(); Theme.ResolveAttribute(Android.Resource.Attribute.WindowBackground, a , true); var windowBackgroundDrawable = Application.Context.GetDrawable(a.ResourceId); var windowBackgroundColor = ((Android.Graphics.Drawables.ColorDrawable)windowBackgroundD‌​rawable).Color; 

Output My Case is : FAFAFA

For ColorPrimary use this :

Code :

Android.Util.TypedValue a = new Android.Util.TypedValue(); Theme.ResolveAttribute(Android.Resource.Attribute.ColorPrimary, a , true); var colorPrimarya = Application.Context.GetDrawable(a.ResourceId); var colorPrimary = ((Android.Graphics.Drawables.ColorDrawable) colorPrimarya).Color; 

Output My Case is : 0072BA

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I saw what i was missing. Your code needed some improvements to get the actual color: var windowBackgroundDrawable = Application.Context.GetDrawable(a.ResourceId); var windowBackgroundColor = ((Android.Graphics.Drawables.ColorDrawable)windowBackgroundDrawable).Color; Could you add it to your solution?
Yes, of cause. Please, update the second snippet as well. So it can be consistent.
And even if it looks pedantic, please fix the prefixes with colorPrimary

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.