0

Let's say I have this theme :

<style name="DummyTheme" parent="AppTheme">
    <item name="android:textColor">@color/white</item>
    <item name="android:windowBackground">@color/black</item>
</style>

Now in my activity I want to get this style's text color and android:windowBackground color propety. I already have this code (the default colors are for debugging purposes) :

TypedArray rootStyle = obtainStyledAttributes(R.style.DummyStyle, new int[]{android.R.attr.textColor, android.R.attr.windowBackground});

@ColorInt int textColor = rootStyle.getColor(0, Color.CYAN);
@ColorInt int backgroundColor = rootStyle.getColor(1, Color.MAGENTA);

rootStyle.recycle();

It works for the text color, but not for the background color :backgroundColor is set to -1. Not Color.MAGENTA, but -1.

I also tried to get a ColorDrawable out of it instead of a color integer but it doesn't work either (its color is set to -1).

Why ? Thanks !

natinusala
  • 596
  • 5
  • 21
  • Cannot find a method with [such a signature](https://developer.android.com/reference/android/content/res/Resources.Theme.html). What overload of `obtainStyledattributes` are you using? – azizbekian Apr 26 '17 at 09:41
  • It's `public final TypedArray obtainStyledAttributes(@StyleRes int resid, @StyleableRes int[] attrs)` – natinusala Apr 26 '17 at 09:42
  • https://stackoverflow.com/questions/12375766/how-to-get-background-color-from-current-theme-programmatically – zxk Jun 25 '19 at 08:59

0 Answers0