How to make custom windowBackground theme style work on all Android devices?
Asked Answered
V

2

11

I am trying to get all of my activities to have a custom theme style that should look like this:

good

This theme works on many devices, including the Nexus 7, the Samsung Galaxy S4, and the Samsung Droid Charge (running Gingerbread). However, on other devices, such as HP Slate 7 and Motorola Droid RAZR, it ends up looking like this:

bad

I have the following in my AndroidManifest.xml's application tag:

android:theme="@style/AppTheme"

The theme is as follows in res/values/styles.xml:

<style name="AppBaseTheme" parent="@android:style/Theme.Light.NoTitleBar">

</style>

<style name="AppTheme" parent="@style/AppBaseTheme">
    <item name="android:windowBackground">@color/background</item>
</style>

I have also added a styles.xml under res/values-v11. Here is the applicable style from there:

<style name="AppBaseTheme" parent="@android:style/Theme.Holo.Light.NoActionBar">
</style>

Finally, this appears in styles.xml under res/values-v14:

<style name="AppBaseTheme" parent="@android:style/Theme.DeviceDefault.Light">
</style>

I have tried to define the theme for each activity and to manually use the setTheme() function in onCreate(), but nothing has worked. I have also tried to manually set a background in every Activity, and this did not work either. What can I do to fix my problem?

EDIT: What's interesting is setting android:background in the styles makes it work, but then elements that should not have backgrounds receive that background color as well.

Verbalize answered 21/4, 2014 at 18:35 Comment(0)
V
20

The key to solving the problem was changing android:windowBackground in the theme to the following:

<item name="android:windowBackground">@drawable/default_background</item>

Notice that I am no longer using @color, but simply a @drawable, the xml for which is below:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/background"/>
</shape>

It seems that some devices do not support accepting a color for this element.

Verbalize answered 21/4, 2014 at 23:0 Comment(1)
I had an S2 running 4.1 which would seemingly randomly choose a colour from my colors.xml for the window background. This solution fixed it for me. I wish I knew why it was broken though!Instability
M
2

I found that the latest version of Android Studio expect that the parent for the style needs to be Theme.AppCompat. Also, it is good style to create a colors.xml file in the values directory (with named color elements between the resource tags). Put your RGB values as values to the named color elements. Reference them in your styles with @color/.

<!--colors.xml in values folder-->
<resources>
    <color name="color1">#ffb62a23</color>
    <color name="color2">#ffedeba6</color>
    <color name="color3">#00ff00</color>
</resources>

<!--style tags -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/color2</item>
    <item name="android:textColorPrimary">@color/color1</item>
    <item name="android:textColorSecondary">@color/color2</item>
    <item name="android:textColorTertiary">@color/color3</item>
</style>
Mcdougal answered 15/8, 2015 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.