How do I change the background color of the ActionBar of an ActionBarActivity using XML?
As per documentation - "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.
Change Background color of the action bar using AppCompat
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background" tools:ignore="NewApi">@color/red</item>
<item name="background">@color/red</item>
</style>
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
you need both android:background
and background
items. The former is for newer versions of android that support ActionBar natively. The latter is for older android versions.
EDIT
instead of <resources>
use
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
From SDK 21
to change Action Bar background color, add this to ActionBarTheme, and the two colours are to be different or it will not work (thanks to @Dre
and @lagoman
)
<item name="colorPrimary">@color/my_awesome_red</item>
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
How do I change the background of the title in the action bar?
If you mean the Toolbar background color, use android:background
on your layout XML. If you mean title font color, use android:textColorPrimary
on your styles.xml file
How to change the color of the actionbar?
You can do it by this way
<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:titleTextStyle">@style/TitleBarTextColor</item>
<item name="android:background">YOUR_COLOR_CODE</item>
</style>
<style name="TitleBarTextColor" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">YOUR_COLOR_CODE</item>
</style>
Obivously, you have to add this theme on your Activity in your AndroidManifest.
EDIT
Add those imports on your Activity
import android.app.ActionBar;
import android.graphics.drawable.ColorDrawable;
Just below on yout onCreate put this
ActionBar ab = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#81a3d0"));
ab.setBackgroundDrawable(colorDrawable);
This works for me :
getActionBar().setBackgroundDrawable(new ColorDrawable(0xff1d97dd));
Let me know if it works :)
Change actionbar background and title color
Styles : Main App Theme
<style name="MyAppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/CustomActionBarStyle</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/CustomActionBarStyle</item>
</style>
Setup styles for text and background color
<style name="CustomActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">@color/white</item>
<item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
<item name="background">@color/white</item>
<item name="titleTextStyle">@style/MyActionBarTitleText</item>
</style>
Setup TextColor Impt: parent must @style/TextAppearance.AppCompat.Widget.ActionBar.Title
<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/action_bar_text_color</item>
</style>
For more refer links posted in @Santosh Shinde post
Android: How to change my action bar background color
The thing is you're extending ActionBarActivity
which comes with AppCompat
package and implementing Widget.Holo.Light.ActionBar
style to it. It is obvious you'll be getting that error.
So do one thing just update your style a little as follow:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimaryDark">@color/primaryDarkColorActionBar</item>
<item name="colorPrimary">@color/primaryColorActionBar</item>
</style>
Also, ActionBarActivity
is deprecated in latest appcompat update i.e. 22.1.1
. So better use AppCompatActivity
instead.
Related Topics
Android Activity Life Cycle - What Are All These Methods For
Sending and Parsing Json Objects in Android
Exception 'Open Failed: Eacces (Permission Denied)' on Android
Saving and Reading Bitmaps/Images from Internal Memory in Android
How to Run a Runnable Thread in Android At Defined Intervals
Accepting Https Connections With Self-Signed Certificates
How to Completely Uninstall Android Studio from Windows(V10)
How to Detect When Wifi Connection Has Been Established in Android
How to Read Contacts on Android 2.0
How to Update a Single Row in a Listview
How to Get a List of Installed Android Applications and Pick One to Run
How to Enable Multidexing With the New Android Multidex Support Library
Android Imageview Scaling and Translating Issue
Difference Between "@Id/" and "@+Id/" in Android