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>
Change Action bar background and text colours programmatically using AppCompat
instead of getActionBar()
use getSupportActionBar()
EDIT:
You are getting errors, because your imports are wrong. Please use the same as below. This works just fine.
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
public class TestActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#95CDBA")));
actionBar.setTitle(Html.fromHtml("<font color='#000099'>Hello World</font>"));
}
}
Yes, this is Google's mistake, it should have a different name. SupportActionBar would be great.
If you are unable to fix the imports, you can explicitly specify which one like this
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
Cannot Change Background Color For Action Bar
Well, after a couple days of fiddling with it, I found out that the problem was mainly due to the fact that android (with its new material theme and all...) changes the color in the actionbar when you change the following properties:
<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:colorPrimary">@color/dark_green</item>
<item name="android:colorPrimaryDark">@color/darker_green</item>
</style>
This properly set the color of the actionbar to @color/dark_green
.
Change the Actionbar Color of an Appcompat Theme
Use colorPrimary to set the color, as in
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/background_green</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/background_green</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">@color/darkgreen_activeTextColor</item>
<style>
See also this blog post from google devs
AppCompat v21 — Material Design for Pre-Lollipop Devices
Change Material Design AppCompat ActionBar Color
There's a new attribute called colorPrimary
which you can define in your Theme. This will give you ActionBar or Toolbar a solid color.
Following a little example:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/my_action_bar_color</item>
</style>
Please note: It has to be only colorPrimary
, not android:colorPrimary
, in every values-folder except the values-v21
one.
You can read more about customizing the Color Palette on developer.android.com.
Related Topics
How to Set Multiple Alarms Using Alarm Manager in Android
Lazy Download Images into Gridview
Calling Setcontentview() Multiple Times
Shadow Effect for a Text in Android
Convert Arraylist<Mycustomclass> to JSONarray
How to Customize Item Background and Item Text Color Inside Navigationview
Check If Correct Google Play Service Available: "Unfortunately Application Has Stopped Working"
Google Places API Request Denied for Android Autocomplete, Even with the Right API Key
Action_Sendto for Sending an Email
Failed to Resolve Com.Android.Support:Appcompat-V7:22 and Com.Android.Support:Recyclerview-V7:21.1.2
How to Crop an Image in Android
Android SQLite Issue - Table ... Has No Column Named
Android Get Current Locale, Not Default
Pick Any Kind of File via an Intent in Android