Skip to content Skip to sidebar Skip to footer

ActionBar Appearing In Different Colors With Different Versions Of Android

I know the question seems simple, but not anything like chei here before. So there you go: In my android application using the Support Library with AppCompat v7, added an ActionBar

Solution 1:

This will not work for you on min target as 2.3.3 in case your min target is API level 11 you can change the background color

<resources>
    <style name="AppTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>

Solution 2:

I would like to add my observation (using min sdk 8, target sdk 19) for the future readers on this issue.

  • If you want to set just the background color of the action bar across different android versions, its easier to do it in code.

    actionBar = getSupportActionBar(); actionBar.setBackgroundDrawable( getResources().getDrawable(R.color.your_desired_color));

This overrides the default background across versions and set your desired color.

Note: I had seen on various post and tried to set the background color as follows:

ColorDrawable colorDrawable = new ColorDrawable();
colorDrawable.setColor(R.color.your_desired_color);
actionBar.setBackgroundDrawable(colorDrawable);

but it did not work, reason unknown to me for the time being.so I successfully tried the code mentioned above.

  • if you want to set the background color of the action bar (along with other attributes) not in code, you can use style and theme. I had done the styling as follows.

inside res/values/style.xml

<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 11 theme customizations can go here. -->
    </style>

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

   <!-- custom theme for my app -->
    <style name="MyAppTheme" parent="AppTheme">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

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

</resources>

inside res/values-v11 and res/values-v14

<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 11 theme customizations can go here. -->
    </style>

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

   <!-- custom theme for my app -->
    <style name="MyAppTheme" parent="AppTheme">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

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

</resources>

Note: As I had to have similar look for the action bar across android versions, I had copy-pasted the code in res/values-v11 and res/values-v14 from res/values. But to my surprise,I did not get the desired result. Reason? We need to add the "android" namespace before the tags for android v11 and later. So I changed actionBarStyle to android:actionBarStyle and background to android:background. That brought me the desired result.


Post a Comment for "ActionBar Appearing In Different Colors With Different Versions Of Android"