Change background color of Android Action Bar - android

I have an Activity with an action bar. minSdkVersion is 11.
The action bar is a grey color, I would like to change it to a different color so that it matches other colors in my application.
I have created the following styles
<style name="MyTheme" 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">#FFF</item>
</style>
And set this activity's theme in the appmanifest to MyTheme but when I run the application I get this error:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
What am I doing wrong?

See https://developer.android.com/training/basics/actionbar/styling.html, specifically these parts
Note: If you are using the Support Library APIs for the action bar,
then you must use (or override) the Theme.AppCompat family of styles
(rather than the Theme.Holo family, available in API level 11 and
higher)
and
When using the Support Library, you must instead use the Theme.AppCompat themes:
Theme.AppCompat for the "dark" theme.
Theme.AppCompat.Light for the "light" theme.
Theme.AppCompat.Light.DarkActionBar for the light theme
with a dark action bar.
Basically you have to replace 'Holo' with 'AppCompat'

If you are using ActionBarActivity, you need to use an AppCompat theme, which brings the Lollipop style Action Bars to all devices and support for the Material Color Palette allowing you to write a theme such as
<style name="MyTheme" parent="#android:style/Theme.AppCompat.Light">
<item name="ColorPrimary">#color/primary</item>
</style>
to style your action bar color automatically.
If you'd prefer to not use any of that (and look very different on Lollipop devices), you can instead extend FragmentActivity or Activity, depending on if you want to use the support library fragments (which backport and fix a number of issues around nested fragments and state saving).

Just use this, if your extending ActivityActionBar on your activity then you need to use the AppCompat themes.
<style name="AppCompatTheme" parent="#android:style/Theme.AppCompat.Light">
<item name="ColorPrimary">#color/primary</item>
</style>
or use style/Theme.AppCompat.Dark for white text :)

Related

Changing status bar icon color

I have received a design spec. in which the status bar icons' color is to be changed completely. I know I can change the status bar color in theme, however that is not my question. I want to override the icon color/icons themselves as shown in the image attached.
I searched a bit but could not find a way to do it.
I am aware that as a last resort, I can change the existing status bar icon tintcolor the following attribute in my app theme.
<item name="android:windowLightStatusBar" tools:targetApi="23">true</item>
You can change it by setting the android:statusBarColor or android:colorPrimaryDark attribute of the style you're using for your app in styles.xml.
(android:statusBarColor inherits the value of android:colorPrimaryDark by default)
For example (since we're using an AppCompat theme here, the android namespace is omitted):
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">#color/your_custom_color</item>
</style>
On API level 21+ you can also use the Window.setStatusBarColor() method from code.
<item name="colorPrimaryDark">#color/your_color</item>
will only be visible in Lollipop and greater than Lollipop(API) devices. P.S. you need to have Theme.AppCompat as your base/main theme

Android actionbar style ignored by later devices, using appcompat v7

I am using the Action Bar support library (appcompat v7), my app is set to a minimum api of 7, and a target of 21.
I have two styles files, a base one, and one targeted at devices api 11+.
When running the app on a device running KitKat, it seems that android:actionBarStyle is ignored, leaving the action bar styled as default (#style/Widget.AppCompat.ActionBar.Solid), instead of applying the given background.
But if I remove my v11 styles/comment them out, KitKat listens to the actionBarStyle attribute set in the base styles.xml file and sets my custom background without any problems.
So my question, where am I going wrong with the v11 styles?
From what I understand, according to the android docs, you are supposed to supply the additional styles for devices running 11+ using the android: prefix, but this just doesn't seem to be working for me.
Stripped down, this is my /res/values/styles.xml file:
<style name="My.Theme" parent="#style/Theme.AppCompat">
<item name="actionBarStyle">#style/ActionBar.Solid</item>
</style>
<style name="ActionBar.Solid" parent="#style/Widget.AppCompat.ActionBar.Solid">
<item name="background">#drawable/ab_solid_</item>
</style>
and this is my /res/values-v11/styles.xml file:
<style name="My.Theme" parent="#style/Theme.AppCompat">
<item name="android:actionBarStyle">#style/ActionBar.Solid</item>
</style>
<style name="ActionBar.Solid" parent="#style/Widget.AppCompat.ActionBar.Solid">
<item name="android:background">#drawable/ab_solid_</item>
</style>
as you can see, the only difference between the two is the use of the android: prefix.
According to the official doc, with the new AppCompat-v21, you can remove all of values-v14+ Action Bar styles and use only one theme declaration, in values:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- Set AppCompat’s actionBarStyle -->
<item name="actionBarStyle">#style/MyActionBarStyle</item>
</style>

ActionBar styling different tab background when below ActionBar

Is there a way to define ActionBar tab style so that it would have different backgrounds when tabs are in ActionBar and below it?
I can't seem to find a way to do that with theme configuration with these properties
<item name="android:actionBarTabStyle">#style/Custom.ActionBar.TabView</item>
<item name="android:actionBarTabBarStyle">#style/Custom.ActionBar.TabBar</item>
<item name="android:actionBarTabTextStyle">#style/Custom.ActionBar.TabText</item>
I can define tab background to be white in #style/Custom.ActionBar.TabBar. All is ok with ActionBar.NAVIGATION_MODE_TABS.
But when I switch to ActionBar.NAVIGATION_MODE_LIST it is placed inside ActionBar and it also becomes white. And thats is unwanted behavior.
You can control the behaviors and visibility of the action bar with the ActionBar APIs, which were added in Android 3.0 (API level 11)."
So, ActionBar will not work for your target environment which is at API level 10 (Android 2.3.3).
Just in case, if you target for minimum API level 11 , you can change ActionBar's background color by defining custom style, as:
<resources>
<style name="MyTheme" 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>
And, set "MyTheme" as theme for application / activity.
Hope this helps...

ActionBarSherlock Back button color change ?

I am the using the ActionBarSherlock. I have the displayOption "homeAsUp" in my style.xml file. Now this shows a black arrow next to the title of the Activity. Since my theme is White on a blue blackground, i want to change the color of the black arrow, or maybe use a whole new icon resource in its place. How can i do this ?
Kind Regards.
Further to Eric's answer - I wasted a lot of time getting this right.
Remember that these items must go in the parent application theme, inheriting from Theme.Sherlock or similar.
<!-- CUSTOM APP THEME -->
<style name="AppTheme" parent="Theme.Sherlock">
<item name="android:actionBarStyle">#style/AppTheme.ActionBarStyle</item>
<item name="actionBarStyle">#style/AppTheme.ActionBarStyle</item>
<item name="android:homeAsUpIndicator">#drawable/action_bar_ic_ab_back_holo_dark</item>
<item name="homeAsUpIndicator">#drawable/action_bar_ic_ab_back_holo_dark</item>
</style>
Do not put them in the custom Action Bar theme inheriting from Widget.Sherlock.ActionBar.
<!-- ACTION BAR THEME -->
<style name="AppTheme.ActionBarStyle" parent="Widget.Sherlock.ActionBar">
<item name="android:icon">#drawable/action_bar_logo</item>
<item name="icon">#drawable/action_bar_logo</item>
<item name="android:displayOptions">showHome</item>
<item name="displayOptions">showHome</item>
</style>
Be careful when you style ActionBarSherlock !
Here is an extract from the web site (ActionBarSherlock Theming):
Due to limitations in Android's theming system any theme customizations must be declared in two attributes. The normal android-prefixed attributes apply the theme to the native action bar and the unprefixed attributes are for the custom implementation. Since both theming APIs are exactly the same you need only reference your customizations twice rather than having to implement them twice.
So in your case you MUST define two item:
<item name="android:homeAsUpIndicator">#drawable/icon</item>
and
<item name="homeAsUpIndicator">#drawable/icon</item>
Then you are sure that ALL your users will have the same L&F

Android: ActionBar Hidden

I have an old app which now when run on tablets like the Xoom, hides the ActionBar. The app was written before the ActionBar existed so some old code is hiding the actionbar.
What in an old app can hide the ActionBar? I need to change it so that the ActionBar and therefore the menu can be shown:
Here's my very simple Theme:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Black">
<item name="android:windowNoTitle">false</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowContentOverlay">#android:color/transparent</item>
</style>
<style name="TextViewStyled" parent="#android:style/Widget.TextView">
<item name="android:textStyle">normal</item>
<item name="android:lineSpacingMultiplier">1.22</item>
</style>
</resources>
And here's the only window settings I use:
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
requestWindowFeature(Window.FEATURE_RIGHT_ICON);
Using android:Theme.Black as your theme parent is what's "hiding" the action bar. The Holo and DeviceDefault theme families include an action bar by default, but the legacy theme family (from which Theme.Black derives) does not.
Use a theme declared with the same name "MyTheme" in res/values-v11/themes.xml with a parent of #android:style/Theme.Holo. This version of the theme will be used on devices running API level 11 (Android 3.0, where Holo and the action bar were introduced) or newer. You should similarly use Widget.Holo.TextView instead of Widget.TextView when declaring a custom view style to work within the Holo theme.

Categories

Resources