0
votes

I have looked at the android documentation and other examples but still cant get my action bar style to work. I have set my theme to "AppTheme" in the android manifest but the background color won't change.

Here is my styles.xml:

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

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

I found out that I need to define the style twice, once using the item name "android:background" and another using the item name "background". The latter is for support library compatibility. I get this error when I run the app:

Error:(11, 41) Color types not allowed (at 'background' with value '#9D0D0D').

Why does android not allow a color with the item name "background" but it allows a color with "android:background"? Is there anything else that could be wrong?

3
Did you try removing android:background and keeping only background?Bidhan

3 Answers

1
votes

Try this code:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimaryDark">@color/primaryDarkColorActionBar</item>
        <item name="colorPrimary">@color/primaryColorActionBar</item>
</style>

Also, ActionBarActivity is deprecated in latest appcompat update i.e. 22.1.1 . So better use AppCompatActivityinstead (If you are not already using it).

1
votes

Solution 1: You can do it from Java. Just use this 2 line after setContentView.

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#db1012")));

Solution 2: You can use Toolbar. Add this to your xml.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/color_primary"
    app:titleTextAppearance="@style/Toolbar.TitleText"
    android:layout_alignParentTop="true">
</android.support.v7.widget.Toolbar>

And this from java.

  /**
 * Set toolbar into actionbar.
 */
protected void setupToolbar() {
    if (toolbar == null) {

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.back);
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setBackgroundColor(Color.RED);
    }
}
0
votes

Adding to Astrount's answer:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimaryDark">@color/primaryDarkColorActionBar</item>
        <item name="colorPrimary">@color/primaryColorActionBar</item>
</style>

In your Gradle configuration dependencies:

dependencies {
    ...
    compile 'com.android.support:design:22.2.0'
}

And in your layout where you've placed your Toolbar:

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

</android.support.design.widget.AppBarLayout>

This automagically styles your Toolbar. Make sure you update to the latest Support Library SDKs with your Android SDK Manager.