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?
}
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?
}
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)
<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 isColorType
. Thanks for your help! –
Potentiometer 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);
Caused by:
in your log cat. Could you please mention that ? It lets you the exact place of error. –
Unhandsome android:color
attribute. –
Unhandsome 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!!**
}
© 2022 - 2024 — McMap. All rights reserved.
?android:attr/colorBackground
corresponds to thestyles.xml
item<item name="android:colorBackground">@color/yourColorHere</item>
. – Beezer