I am trying to get all of my activities to have a custom theme style that should look like this:
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:
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.
To change default themes go to File and click on Settings. A new Settings dialog will appear, like this. Under the Appearance & Behaviour -> Appearance, you will find Theme. Choose the right theme from the drop-down and click on Apply and then Ok.
Create and apply a style To create a new style or theme, open your project's res/values/styles. xml file. For each style you want to create, follow these steps: Add a <style> element with a name that uniquely identifies the style.
In the Project pane select Android, go to app > res > values > themes > themes.
Go to File > Settings, now under IDE settings click on appearance and select the theme of your choice from the dropdown. File > Import Settings , select the file or your choice and select ok a pop up to restart the studio will open up click yes and studio will restart and your theme will be applied.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With