2

I have this style.xml inside my Android app:

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<style name="OrangeTheme" parent="@style/Theme.AppCompat">
    <item name="android:actionBarStyle" tools:ignore="NewApi">@style/OrangeTheme.OrangeActionBar</item>
    <item name="android:windowActionBar">true</item>
</style>

<style name="OrangeTheme.OrangeActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">@color/orange</item>
    <item name="background">@color/orange</item>
    <item name="android:backgroundStacked">@color/orange</item>
    <item name="android:backgroundSplit">@color/orange</item>
    <item name="android:titleTextStyle">@style/Theme.MyAppTheme.ActionBar.TitleTextStyle</item>
</style>

<style name="Theme.MyAppTheme.ActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/black</item>
</style>

I have searched StackOverflow and Google, but nothing helped. I wasn't able to change the ActionBar background color.

Inside my MainActivity, I use this code:

 public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}

I tried not to use AppCompat, but then my name has a lot of errors. It extends ActionBarActivity. If I change it to FragmentActivity I can't use getSupportActionBar() and without it my getActionBar(), is always null.

minSdkVersion 19
targetSdkVersion 21

So right now I am not able to change it. I tried renaming the parent of my style theme. I tried using andrioud:background and background. The only thing that is changing is when I use another parent like Theme.App.Compat.Light or Theme.AppCompat.Light.DarkActionBar.

Does someone have an idea, how I could be able to change the colors and other stuff, on runtime?

2

2 Answers 2

0

With new AppCompat-v7 (v21) you can use new color theming attributes. See here.

Or alternatively you have to use <item name="actionBarStyle"> property (without prefix android).

0

To use background color with AppCompat, you need to also define the item name without the android: attribute.

for example:

<style name="MyActionBar" parent="Theme.AppCompat.Light">
    <item name="android:background">@color/action_bar_background</item>

    <item name="background">@color/action_bar_background</item> //<--background item without the android attribute
</style>

and then in your custom theme do the same thing for the custom actionbar style:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.