Android ActionBar does not change background color

I am trying to change the background color of my action bar, but it does not change. My app uses the AppCompat theme if it affects anything.

Here is my theme that is being used. My application points to the use of this theme, so this is not a 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> 

+6
source share
3 answers

In the ActionBar style, remove android: background, save only the background attribute. Also make sure that you use AppTheme in the application tag or in actions that extend the action bar in your AndroidManifest.xml file. I also added the attribute "@ style /" before Widget.AppCompat with its 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> 
+14
source

You can also do this from code.

ab.setBackgroundDrawable (new ColorDrawable (newcolor));

Just in case, your xml is not working

0
source

According to this link https://developer.android.com/reference/android/R.attr.html#background it does not seem to accept a color resource, so you either give it a color value of hex color, or create a drawable that uses the color you need. You can 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> 
0
source

Source: https://habr.com/ru/post/974663/


All Articles