3

I have 2 Themes, a dark and a bright one. I want to get the current themes background color and use it in a layout file like this:

<View
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="(backgroundColor)" />

Let's say (backgroundColor) is the current themes background color. What do I have to put in there instead of (backgroundColor)?

King Natsu
  • 461
  • 4
  • 19

2 Answers2

4

The correct answer to your question would probably be?android:colorBackground. But you might still want to use ?android:windowBackground on some occasions.

While windowBackground will be the background of your activity, colorBackground seems like the default background color for your content.

In your case you would end up like the following:

<View
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="?android:colorBackground" />

For a full list of those "default" theme attributes you can check out the source code of the basic Android themes.xml

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
3

use android:theme

<View
    android:theme="@style/yourTheme"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

and in your values\styles.xml

 <style name="yourTheme" parent="AppTheme">
        <item name="android:background">@color/green</item>
 </style>

Edit: Please try to post your full question at the first time and if you make a change try to add it under edit section. People who love to help you gets in trouble otherwise.

For your edited question already has an answer here

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.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) {
    // windowBackground is a color
    int color = a.data;
} else {
    // windowBackground is not a color, probably a drawable
    Drawable d = activity.getResources().getDrawable(a.resourceId);
}

and set it to your view!

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • Sorry about the inconvenience. I'm sorry to tell you, but I can't code in this matter, because it would turn my layouts and code into a mess. Thank you anyway – King Natsu Feb 25 '17 at 21:52