Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBar background with MaterialComponents theme

I want to customize my ActionBar. My Theme looks as below:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/ThemeOverlay.MaterialComponents.ActionBar">
    <item name="background">@drawable/actionBarBackground</item>
</style>

In values folder:

<drawable name="actionBarBackground">#FFFFFFFF</drawable>

In values-night folder:

<drawable name="actionBarBackground">#FF000000</drawable>

Not sure why my ActionBar background colour doesn't change accordingly. I have tried to change my theme in different other ways as well but nothing works. Here are my other tries:

Instead of actionBarStyle, I used actionBarTheme.

<item name="actionBarTheme">@style/MyActionBar</item>

I also tried using colorPrimary.

<item name="colorPrimary">@color/actionBarBackground</item>

Am I missing something?

like image 772
Chandra Sekhar Avatar asked Oct 13 '19 11:10

Chandra Sekhar


Video Answer


1 Answers

Since you are using a Theme.MaterialComponents.DayNight app theme the ActionBar background color is defined by default by the colorPrimary attribute.

  <style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
    <item name="colorPrimary">@color/mycolor</item>
  </style>

where mycolor is define in res/values/colors.xml

<resources>
  <color name="mycolor">#xxxxxx</color>
  ...
</resources>

You can also customize the background color using the actionBarStyle attribute in your app theme.
Something like:

  <style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
      <item name="actionBarStyle">@style/Custom.ActionBar</item>
      ...
  </style>

where:

  <style name="Custom.ActionBar" parent="Widget.MaterialComponents.Light.ActionBar.Solid">
    <item name="background">@color/mycolor</item>
  </style>

Otherwise you can use the actionBarTheme attribute to override the background color:

  <style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
    <item name="actionBarTheme">@style/ThemeOverlay.ActionBar</item>
    ...
  </style>

  <style name="ThemeOverlay.ActionBar" parent="">
    <item name="colorPrimary">@color/mycolor</item>
  </style>

enter image description here

like image 177
Gabriele Mariotti Avatar answered Sep 19 '22 02:09

Gabriele Mariotti