actionBar background doesn't change - android

So I'm new to app developing and currently following a developer's guide on android.com. I'm stuck on "stylizing your actionBar" part, because the code suggested on the website doesn't work:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_background</item>
</style>
</resources>
I get the compatibility error about android:backgound that can't be used since my minSdk is 7 (though the guide claims it should work).
I don't want to change my minSdk, and I tried splitting the themes.xml into 2, one I put into values, and the other into values-v11. The error doesn't occur anymore, but when I run the app the actionBar doesn't change its backgroud. Here are the both files:
this one is in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_background</item>
</style>
</resources>
and this one is in values-11
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#FFB300</item>
</style>
</resources>
Do you have any idea what's the problem here?
The API lvl on my device is 21

As of AppCompat v21, the android: attributes are no longer used. It also supports color theming, which provides a very easy way to set the color of the action bar (and status bar on Android 5.0 devices) using a very simple theme:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#color/my_awesome_darker_color</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight & colorSwitchThumbNormal. -->
</style>

Related

How can I change statusbar color without a toolbar?

I'm trying to make the status bar color #a1a1a1 (white_dark) but it's just black when i run it.
This is my Style v21 file:
<style name="AppBaseTheme" parent="AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#color/white_dark</item>
</style>
you can try this lib
But my personal suggestion is, switch to AppCompatbecause its very easy to change StatusBar color in it.
Primary Dark color is used for StatusBar:
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#android:color/black</item>
use this theme
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/atctionbar_color</item>
<!-- Support library compatibility -->
<item name="background">#color/atctionbar_color</item>
</style>
</resources>
and create new file in values>colors.xml and add color from there
<resources>
<color name="atctionbar_color">#170053</color>
</resources>

How to change ActionBar background color in API level 21

I add following code, but it is for API level 14
<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>
Please help me to find the solution.
<resources>
<!-- inherit from the material theme -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">#color/primary</item>
</style>
</resources>
Taken from Android developer site.
You need to basically use the colorPrimary value to denote Action Bar color. There are others as well to show status bar color and all, which you can find in the link.
This is what I have used to do the job, but I was using AppCompat.
<!-- Base application theme. -->
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="CustomActionBarTheme" parent="#style/AppTheme">
<item name="actionBarStyle">#style/CustomActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="CustomActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#drawable/actionbar_foreground</item>
</style>
And make a drawable called actionbar_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF0000"/>
</shape>
Don't forget to update AndroidManifest.xml
<application
android:theme="#style/CustomActionBarTheme">
ToolBar.setBackgroundColor(Color.BLACK);
It works for me in API 21:
if you use AppCompatActivity
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.<YOUR_COLOR>)));

Changing the colour of ActionBar in Android

Here is my xml code in styles.xml to change theme and colour of ActionBar.
<?xml version="1.0" encoding="utf-8"?>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#drawable/brand_logo</item>
<item name="android:background">#ff5d44</item>
</style>
Here is the documentation. My app supports API level 11 and higher. Not sure where I am going wrong. Please help.
set "AppTheme" as theme for application / activity.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#android:color/white</item>
</style>
For more check THIS ,THIS and THIS
I was using support library. So here is the fix.
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#ff5d44</item>
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
<!--Support library compatibility-->
<item name="background">#color/orange</item>
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
</style>

Android ActionBar background change don't work [duplicate]

This question already has an answer here:
Change Android 5.0 Actionbar color
(1 answer)
Closed 7 years ago.
I'm trying to change background color of action bar in my app and my styles.xml looks that:
<resources>
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimary">#71343e</item>
<!-- theme UI controls like checkboxes and text fields -->
<!-- native widgets will now be "tinted" with accent color -->
<item name="colorAccent">#ff71bf41</item>
<!--Action bar style-->
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="actionBarStyle">#style/AppTheme.ActionBar</item>
</style>
<style name="AppTheme.ActionBar" parent="Widget.AppCompat.Light.ActionBar">
<item name="titleTextStyle">#style/AppTheme.ActionBar.TitleText</item>
<item name="android:titleTextStyle">#style/AppTheme.ActionBar.TitleText</item>
<item name="android:height">100dp</item>
<item name="android:colorBackground">#71343e</item>
</style>
<style name="AppTheme.ActionBar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#0d5875</item>
</style>
I'have tryed to use various color argument but anyone work. How I can change the ActionBar color?
For android version 3.0+, this is one solution:
inside res/values/themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
</style>
</resources>
And set it to your whole application (or individual activities)
<application android:theme="#style/CustomActionBarTheme" ... />
For android version 2.1+, you have to modify it a bit
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_background</item>
</style>
</resources>
And
<application android:theme="#style/CustomActionBarTheme" ... />
You can find more information about styling the ActionBar here.

Theme not applying to emulator device

I have a one activity and one layout application. I am implementing ActionBarSherlock, but my theme isn't applying to my emulator/early devices.
My theme.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActivityTheme" parent="Theme.Sherlock">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- other activity and action bar styles here -->
</style>
<!-- style for the action bar backgrounds -->
<style name="MyActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="android:backgroundSplit">#4200ff</item>
<item name="android:backgroundStacked">#4200ff</item>
<item name="android:background">#4200ff</item>
</style>
</resources>
This causes the ActionBar on my JellyBean device to turn blue, but it stays black on my 2.2 emulator.
Here is a piece from my manifest.xml:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/CustomActivityTheme">
In Android version 3.0 and later you can assign theme to to item name android:actionBarStyle. But in 2.3 and earlier there is no Action Bar, so you can't use item with name android:actionBarStyle. To fix this ActionBarSherlock has defined it's own item name just actionBarStyle without android prefix.
So your code should look like this
<!-- the theme applied to the application or activity -->
<style name="CustomActivityTheme" parent="Theme.Sherlock">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
<!-- other activity and action bar styles here -->
</style>
<!-- style for the action bar backgrounds -->
<style name="MyActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="android:backgroundSplit">#4200ff</item>
<item name="android:backgroundStacked">#4200ff</item>
<item name="android:background">#4200ff</item>
</style>

Categories

Resources