how to get background color from current theme programmatically
Asked Answered
P

3

41

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?
}
Protrusile answered 11/9, 2012 at 18:31 Comment(3)
at xml android:background="?android:attr/colorBackground"Kasandrakasevich
I tested and determined that ?android:attr/colorBackground corresponds to the styles.xml item <item name="android:colorBackground">@color/yourColorHere</item>.Beezer
@Kasandrakasevich this should be the answer!!!!Jacinthe
S
76

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)
Sinistrous answered 22/1, 2013 at 21:9 Comment(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.Retouch
pbarranis, When defining a style xml, most of my colors are referenced with unqualified names (<item name="someColor>) and are declared in my own attrs.xml; but the background color is qualified (<item name="android:background">), 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.Potentiometer
@ashuhes FYI, I updated your answer to use the latest API function, isColorType. Thanks for your help!Potentiometer
@Potentiometer Good to know! Thanks for updating it.Sinistrous
U
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);
Unhandsome answered 11/9, 2012 at 23:26 Comment(8)
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.Beep
Oh! What exception do you get ?Unhandsome
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)...Beep
There would be a line Caused by: in your log cat. Could you please mention that ? It lets you the exact place of error.Unhandsome
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) atBeep
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) atBeep
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: <item> 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 moreBeep
It says that in the Binary XML file at line 18, the <item> tag is missing android:color attribute.Unhandsome
M
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!!**
}
Macias answered 19/10, 2017 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.