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><stylename="AppTheme"parent="@android:style/Theme.Holo.Light"><itemname="android:actionBarStyle">@style/MyActionBar</item></style><stylename="MyActionBar"parent="@android:style/Widget.Holo.Light.ActionBar"><itemname="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:

ColorDrawablecolorDrawable=newColorDrawable();
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.
    --><stylename="AppBaseTheme"parent="Theme.AppCompat.Light.DarkActionBar"><!-- API 11 theme customizations can go here. --></style><!-- Application theme. --><stylename="AppTheme"parent="AppBaseTheme"><!-- All customizations that are NOT specific to a particular API-level can go here. --></style><!-- custom theme for my app --><stylename="MyAppTheme"parent="AppTheme"><itemname="android:actionBarStyle">@style/MyActionBar</item></style><!-- ActionBar styles --><stylename="MyActionBar"parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"><itemname="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.
    --><stylename="AppBaseTheme"parent="Theme.AppCompat.Light.DarkActionBar"><!-- API 11 theme customizations can go here. --></style><!-- Application theme. --><stylename="AppTheme"parent="AppBaseTheme"><!-- All customizations that are NOT specific to a particular API-level can go here. --></style><!-- custom theme for my app --><stylename="MyAppTheme"parent="AppTheme"><itemname="android:actionBarStyle">@style/MyActionBar</item></style><!-- ActionBar styles --><stylename="MyActionBar"parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"><itemname="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"