40

I tried something like this, but i stuck:

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true))
{
  // how to get color?
}
Bartłomiej Mucha
  • 2,762
  • 1
  • 30
  • 36

3 Answers3

75

You can get the background color (or Drawable) from the current theme by:

TypedValue a = new TypedValue();
getTheme().resolveAttribute(android.R.attr.windowBackground, a, true);
if (a.isColorType()) {
    // windowBackground is a color
    int color = a.data;
} else {
    // windowBackground is not a color, probably a drawable
    Drawable d = activity.getResources().getDrawable(a.resourceId);
}

isColorType was introduced in API level 29. Before then, you can use the following instead:

if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT)
jpaugh
  • 6,634
  • 4
  • 38
  • 90
ashughes
  • 7,155
  • 9
  • 48
  • 54
  • 4
    Great answer. In case anyone else is tripped up by this, on Xamarin you have to use Resource.Attribute.primaryAccentColor instead of, for instance, Resource.Styleable.MyTheme_primaryAccentColor. This probably applies to Android native development too. That is, reference the attr file/object instead of the theme directly. I don't understand the difference, but the latter seems like it would be the right choice but was NOT. – pbristow Feb 12 '16 at 17:14
  • pbarranis, When defining a style xml, most of my colors are referenced with unqualified names (``), and it's declared in the Android system somewhere, not in my own `attrs.xml`, like the others. Therefore, the background color is not a property of *my* app, but a property of *any/every* android app. That's the reason for the difference. – jpaugh Jan 04 '21 at 05:50
  • @ashuhes FYI, I updated your answer to use the latest API function, `isColorType`. Thanks for your help! – jpaugh Jan 04 '21 at 06:08
  • @jpaugh Good to know! Thanks for updating it. – ashughes Jan 10 '21 at 08:06
7

You can get the resources of your Theme by using :

TypedArray a = getTheme().obtainStyledAttributes(R.style.ThemeName, new int[] {R.attr.attribute_name});     
int attributeResourceId = a.getResourceId(0, 0);
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • I tried this: TypedArray a = this.parentActivity.getTheme().obtainStyledAttributes(android.R.attr.windowBackground, new int[] {android.R.attr.windowBackground}); int attributeResourceId = a.getResourceId(0, 0); int aaa = this.parentActivity.getResources().getColor(attributeResourceId); but this is not working. I get exception. – Bartłomiej Mucha Sep 12 '12 at 06:47
  • Oh! What exception do you get ? – Swayam Sep 12 '12 at 08:50
  • 09-12 12:05:34.864: E/AndroidRuntime(32137): FATAL EXCEPTION: main 09-12 12:05:34.864: E/AndroidRuntime(32137): android.content.res.Resources$NotFoundException: File res/drawable/screen_background_selector_light.xml from color state list resource ID #0x10804a8 09-12 12:05:34.864: E/AndroidRuntime(32137): at android.content.res.Resources.loadColorStateList(Resources.java) 09-12 12:05:34.864: E/AndroidRuntime(32137): at android.content.res.Resources.getColor(Resources.java)... – Bartłomiej Mucha Sep 12 '12 at 10:06
  • There would be a line `Caused by: ` in your log cat. Could you please mention that ? It lets you the exact place of error. – Swayam Sep 12 '12 at 17:17
  • FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: File res/drawable/screen_background_selector_light.xml from color state list resource ID #0x10804a8 at android.content.res.Resources.loadColorStateList(Resources.java) at android.content.res.Resources.getColor(Resources.java) at com.mycustomapp.NewsDetailsFragment.onCreateView(NewsDetailsFragment.java:64) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083) at – Bartłomiej Mucha Sep 13 '12 at 07:45
  • android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420) at android.os.Handler.handleCallback(Handler.java) at android.os.Handler.dispatchMessage(Handler.java) at android.os.Looper.loop(Looper.java) at android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at – Bartłomiej Mucha Sep 13 '12 at 07:46
  • com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) at dalvik.system.NativeStart.main(Native Method) Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #18: tag requires a 'android:color' attribute. at android.content.res.ColorStateList.inflate(ColorStateList.java) at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java) at android.content.res.ColorStateList.createFromXml(ColorStateList.java) ... 17 more – Bartłomiej Mucha Sep 13 '12 at 07:46
  • 2
    It says that in the Binary XML file at line 18, the tag is missing `android:color` attribute. – Swayam Sep 13 '12 at 16:15
6

for your qoustion the easiest way is:

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true))
{
  // how to get color?
  int colorWindowBackground = typedValue.data;// **just add this line to your code!!**
}
batsheva
  • 2,175
  • 1
  • 20
  • 32