2

I'm trying to change the background colour of my Android app, but I'm not being able to. I've searched through a lot of posts but none solution solved mine.

Here's mine styles.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#ff0000</item>
</style>


</resources>

As you can see, I'm just trying to set the bar to red. Here's my manifest:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MenuActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

The theme is properly set, as you can see. And just so you can see, here's my gradle:

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
    ...
    minSdkVersion 15
    targetSdkVersion 21



}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

So, even though this looks alright, it is not setting my ActionBar to red. I tried following a lot of guides (including the official one) and I've done everything as they say. What am I missing? Thank you for your patience in advance :)

bnoite
  • 185
  • 2
  • 12

2 Answers2

9

Since you are using the AppCompat library, you should handle older android versions too (just like it was when using the ActionBarSherlock library):

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/ActionBar</item>
        <item name="actionBarStyle">@style/ActionBar</item>
    </style>

    <style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@android:color/red</item>
        <item name="background">@android:color/red</item>
    </style>
</resources>

The items without the android namespace are for styling the appcompat actionbar used on older android versions.

See this answer for further reference.

You should also check, that you probably have more styles.xml files generated for various screen sizes and sdk version.

You may want to add your ActionBar style declarations to these other styles too: it looks like your test device uses another one, not the default.

Community
  • 1
  • 1
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • 1
    Thank you for the fast response. I only have one styles.xml, situated inside the "values" folder. The other "values" folder that I have is "values-w820dp" that only has "dimens.xml" inside :( – bnoite Dec 18 '14 at 22:03
  • It seems that you are using an older android version for testing, please see my answer on how to proceed to make it work – rekaszeru Dec 18 '14 at 22:32
  • 1
    It worked! The only change I had to make to your answer was to change the colour in the #ff0000 It doesn't accept HEX as an argument. Thank you for your help! – bnoite Dec 18 '14 at 23:14
  • I'm glad I could help. I'm going to update the answer based on your note, so others could learn from that too. Thanks! – rekaszeru Dec 18 '14 at 23:18
  • Thank you a lot. I search all the net and try all solution but just this work. thank you. – Mohammad Hossein Dolatabadi Aug 09 '21 at 10:46
0

<style name="Demo_Theme" parent="Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/actionBG</item>
    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/actionBG</item>
</style>

<style name="actionBG" parent="Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@color/test_bg_color</item>
    <!-- Support library compatibility -->
    <item name="background">@color/test_bg_color</item>
</style>

This should work fine.

nishant.v
  • 1
  • 4