Android ActionBar not changing background color - android

I am trying to change the background color of my action bar but it doesnt change. My app is using the AppCompat theme if that affects anything.
Here is my theme that is being used. My app points to use this theme so that is not the problem
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/ActionBar</item>
</style>
<!-- Action Bar Styles -->
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
<item name="android:background">#color/blue</item>
<item name="background">#color/blue</item>
</style>

In your ActionBar style, delete the android:background, only keep the background attribute. Also make sure to implement your AppTheme in the application tag or in the activities extending the action bar in your AndroidManifest.xml file. I've also added the "#style/" attribute before Widget.AppCompat since its a default style defined in the android library.
Your code should look like this :
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/ActionBar</item>
</style>
<!-- Action Bar Styles -->
<style name="ActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="background">#0000ff</item>
</style>

You can se this from code as well
ab.setBackgroundDrawable(new ColorDrawable(newcolor));
Just in case your xml is not working

According to this link, https://developer.android.com/reference/android/R.attr.html#background
it doesn't seem to take color resource, so you either give it a hex color value or create a drawable that uses the color you want. You could try the following alternative to see if it works.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/ActionBar</item>
</style>
<!-- Action Bar Styles -->
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
<item name="android:background">#0000ff</item>
<item name="background">#0000ff</item>
</style>

Related

When trying to use the material theme on Android, my title bar is not using the primary colour

From my research, it appears that when using the material theme on Android the title bar should take on the primary color but for me, it stays black. I know the theme itself is working as things did change to black when I first applied the default(dark) theme. I have this set in the styles.xml file:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
</style>
</resources>
The colors referenced from colors.xml are working as they show up on the s the de in android studio.
I have tried looking for a way to manually change the title bar but I have only found ways to do it programatically and not through layout files. I have attached a pic of what the title bar currently looks like and what i would like it to look like below:
Current title bar
What I want it to look like
Change it to
<style name="AppTheme" parent="android:Theme.Material">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
</style>

Textview background color got changed after adding custom theme to android app

My app interface originally looked like this. "my name" is a TextView and "my button" is a button which has an image as the background. Minimum sdk is 16.
I changed the action bar color of the app by adding a custom theme. I used the code below.
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/MyTheme</item>
<!-- Customize your theme here. -->
</style>
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:background">#color/com_facebook_blue</item>
</style>
Then my output came like this.
All the background color got changed to the new color of the app. I only need to change the color of the action bar.
Can anyone help me?
Try this
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>

How to use android:Theme.Material (Material theme) in styles.xml android?

In my application, I am trying to implement android:Theme.Material as a parent theme in styles values-21 folder:
<!-- res/values-21/styles.xml -->
<resources>
<!-- your theme inherits from the material theme -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- theme customizations -->
<item name="android:colorPrimary">#color/primary</item>
<item name="android:textColorPrimary">#color/text_primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:colorAccent">#color/accent</item>
<item name="android:navigationBarColor">#color/primary_dark</item>
</style>
</resources>
After running the app, I am getting below error
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
In values folder. I have below style
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
But, if I add the same Theme.AppCompat.Light in values-21 folder its working fine. but actionbar color is not changing.
Why can't i use the material design theme in values-21 folder?
How to solve this problem?
(note: my application minsdk verison is 13 and maxsdk version is 22)
My activity extends AppCompactActivity
Your app theme is defined in the manifest file:
<application
android:theme="#style/AppTheme">
You will find this style defined in /res/values/styles.xml.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Using AppCompat allows your themes to be used even for devices older than Android 5.0. The main Material Design Themes are shown below.
Dark Theme
Light Theme
Light Theme with Dark Action Bar
Further Reading
Using the Material Theme (Android docs)
If your are using an AppCompatActivity, just use only a style in your res/values/styles.xml, and check the namespace that your are using for colorPrimary, colorPrimaryDark....
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>

How to set background color of android app and background of action bar

I am trying to modify my app to have a dark blue action bar with a light blue background. So far I have only been able to make the entire app dark blue. How can I modify my code to keep the action bar the color it is now but make the background with the team numbers a light blue? Please note my apps minimum API is 14.
I am currently using the following code in my styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:background">#color/teamLightBlue</item>
</style>
<!-- Activity themes -->
<style name="Theme.Base" parent="android:Theme.Light" />
<style name="Theme.Sample" parent="Theme.Base" />
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="AppTheme">
<item name="android:actionBarStyle">#style/MyActionBarTheme</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBarTheme" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/teamDarkBlue</item>
</style>
If you choose to use Theme.AppCompat, you can change Action Bar's background color using:
<item name="colorPrimary">#ffffff</item>
but if you choose the Holo one, you can change that color using
<item name="android:colorPrimary">#ffffff</item>
Hope this helps.
As far as I know your code is good instead of Widget.Holo.Light.ActionBar as its not part of AppCompat v7. Use this instead
<style name="CustomActionBarTheme" parent="AppTheme">
<item name="android:actionBarStyle" tools:ignore="NewApi">#style/MyActionBarTheme</item>
<item name="actionBarStyle">#style/MyActionBarTheme</item>
</style>
<style name="MyActionBarTheme" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/teamDarkBlue</item>
<item name="background">#color/teamDarkBlue</item>
</style>
It should work fine!

How to set custom ActionBar color / style?

I am using Android Navigation bar in my project,
I want to change the top color in action bar to something red, How can i do that?
I have something like this,
and i want something like this,
how can i achieve that?
You can define the color of the ActionBar (and other stuff) by creating a custom Style:
Simply edit the res/values/styles.xml file of your Android project.
For example like this:
<resources>
<style name="MyCustomTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBarTheme</item>
</style>
<style name="MyActionBarTheme" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">ANY_HEX_COLOR_CODE</item>
</style>
</resources>
Then set "MyCustomTheme" as the Theme of your Activity that contains the ActionBar.
You can also set a color for the ActionBar like this:
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED)); // set your desired color
Taken from here: How do I change the background color of the ActionBar of an ActionBarActivity using XML?
In general Android OS leverages a “theme” to allow app developers to globally apply a universal set of UI element styling parameters to Android applications as a whole, or, alternatively, to a single Activity subclass.
So there are three mainstream Android OS “system themes,” which you can specify in your Android Manifest XML file when you are developing apps for Version 3.0 and later versions
I am referring the (APPCOMPAT)support library here:--
So the three themes are
1. AppCompat Light Theme (Theme.AppCompat.Light)
AppCompat Dark Theme(Theme.AppCompat),
And a hybrid between these two ,AppCompat Light Theme with the Darker ActionBar.( Theme.AppCompat.Light.DarkActionBar)
AndroidManifest.xml and see the tag, the android theme is mentioned as:-- android:theme="#style/AppTheme"
Open the Styles.xml and we have base application theme declared there:--
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
We need to override these parent theme elements to style the action bar.
ActionBar with different color background:--
To do this we need to create a new style MyActionBar(you can give any name) with a parent reference to #style/Widget.AppCompat.Light.ActionBar.Solid.Inverse that holds the style characteristics for the Android ActionBar UI element. So definition would be
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#color/red</item>
</style>
And this definition we need to reference in our AppTheme, pointing to overridden ActionBar styling as--
#style/MyActionBar
Change the title bar text color (e.g black to white):--
Now to change the title text color, we need to override the parent reference parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
So the style definition would be
<style name="MyActionBarTitle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>
We’ll reference this style definition inside the MyActionBar style definition, since the TitleTextStyle modification is a child element of an ActionBar parent OS UI element. So the final definition of MyActionBar style element will be
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#color/red</item>
<item name="titleTextStyle">#style/MyActionBarTitle</item>
</style>
SO this is the final Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- This is the styling for action bar -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#color/red</item>
<item name="titleTextStyle">#style/MyActionBarTitle</item>
</style>
<style name="MyActionBarTitle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>
</resources>
For further ActionBar options Menu styling, refer this link
For Android 3.0 and higher only
When supporting Android 3.0 and higher only, you can define the action bar's background like this:
res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#ff0000</item>
</style>
</resources>
For Android 2.1 and higher
When using the Support Library, your style XML file might look like this:
<?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>
Then apply your theme to your entire app or individual activities:
for more details Documentaion
You can change action bar color on this way:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/green_action_bar</item>
</style>
Thats all you need for changing action bar color.
Plus if you want to change the status bar color just add the line:
<item name="android:colorPrimaryDark">#color/green_dark_action_bar</item>
Here is a screenshot taken from developer android site to make it more clear, and here is a link to read more about customizing the color palete
Another possibility of making.
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
I can change ActionBar text color by using titleTextColor
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="titleTextColor">#333333</item>
</style>
Custom Color:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/ColorPrimary</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Customize your theme here. -->
</style>
Custom Style:
<style name="Theme.AndroidDevelopers" parent="android:style/Theme.Holo.Light">
<item name="android:selectableItemBackground">#drawable/ad_selectable_background</item>
<item name="android:popupMenuStyle">#style/MyPopupMenu</item>
<item name="android:dropDownListViewStyle">#style/MyDropDownListView</item>
<item name="android:actionBarTabStyle">#style/MyActionBarTabStyle</item>
<item name="android:actionDropDownStyle">#style/MyDropDownNav</item>
<item name="android:listChoiceIndicatorMultiple">#drawable/ad_btn_check_holo_light</item>
<item name="android:listChoiceIndicatorSingle">#drawable/ad_btn_radio_holo_light</item>
</style>
For More: Android ActionBar
As I was using AppCompatActivity above answers didn't worked for me. But the below solution worked:
In res/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
</style>
PS: I've used colorPrimary instead of android:colorPrimary
in the style.xml add this code
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyAction</item>
</style>
<style name="MyActionBarTheme" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#333333</item>
</style>
</resources>
Use this - http://jgilfelt.github.io/android-actionbarstylegenerator/
Good tool to customize your actionbar with a live preview in couple of minutes.

Categories

Resources