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.

kalitsov
  • 1,319
  • 3
  • 20
  • 33

1 Answers1

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

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • 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? – kalitsov Nov 24 '16 at 13:18
  • Yes, of cause. Please, update the second snippet as well. So it can be consistent. – kalitsov Nov 24 '16 at 13:27
  • And even if it looks pedantic, please fix the prefixes with colorPrimary – kalitsov Nov 24 '16 at 13:31