Bolun Zhang
Bolun Zhang

Reputation: 148

Android ActionBar background colors.xml not working

I'm a beginner in android development. And I'm following the training on developer.android.com. But I'm having problem changing actionbar background color right now. I have the error

Description Resource    Path    Location    Type
error: Error: No resource found that matches the given name (at 'android:background' with value '@colors/orange').  styles.xml  /ActionBarColor/res/values  line 17 Android AAPT Problem

when I'm refering to res/values/colors.xml from my res/values/styles.xml

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name ="orange">#FF8800</color>
</resources>

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
        <item name="actionBarStyle">@style/myActionBar</item>
    </style>

    <style name = "myActionBar" parent = "Widget.AppCompat.Light.Base.ActionBar.Solid.Inverse">
        <item name ="android:background">@colors/orange</item> <!--Error Here-->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

I read almost every thread on this but found no clue. My code looks exactly same as the others, as far as I can tell. Please help me. The project target is API19, min ver is API11

added themes.xml as the website says, error stays

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">

        <item name="android:background">@colors/orange</item>

        <!-- Support library compatibility -->
        <item name="background">@colors/orange</item>
    </style>
</resources>

Upvotes: 1

Views: 2196

Answers (1)

arielnmz
arielnmz

Reputation: 9145

As I can see, you're using the AppCompat library on your Android project, so you should only use AppCompat resources for theming. Right from the Android training pages on how to Style the Action Bar, in the section "Customize the Background":

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>

        <!-- Support library compatibility -->
        <item name="background">@drawable/actionbar_background</item>
    </style>
</resources>

Then apply your theme to your entire app or to individual activities:

<application android:theme="@style/CustomActionBarTheme" ... />
  1. Create an style for the Action Bar based on any existent AppCompat themes and define the background item (for appcompat apps) - MyActionBar in the above example.
  2. Create an style that'll be used by your project that applies the previously created Action bar style, and define the actionBarStyle item (for appcompat apps) - CustomActionBarTheme in the above example.

You would really want to read through the Android Training Pages, they're pretty well explained; and you also have the Android API Guides and the Android API Reference Pages just in case.

Upvotes: 2

Related Questions