Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the background color of the ActionBar in the new MaterialComponents

I'm using the new MaterialComponents and my app theme is:

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryDarkColor</item>
    <item name="colorAccent">@color/secondaryColor</item>
</style>

how to change the background color of ActionBar

I know about this question but it's outdated and doesn't use MaterialComponents

I don't want to add Toolbar to every screen just to change the background color.

like image 584
humazed Avatar asked Feb 28 '19 20:02

humazed


People also ask

How can change ActionBar background color in android programmatically?

To change background color of Action Bar in Kotlin Android, set the colorPrimary in themes. xml, with a required color. We can also dynamically change the background color of Action Bar programmatically by setting background drawable for support action bar with the required color drawable.

How do I change the color of the bar on my Android?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar. Step 3: In your MainActivity, add this code in your onCreate method.


1 Answers

You need use ThemeOverlay.MaterialComponents.ActionBar or ThemeOverlay.MaterialComponents.Dark.ActionBar depending on the background color.

  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryDarkColor</item>
        <item name="colorAccent">@color/secondaryColor</item>
        <item name="actionBarTheme">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@style/ThemeOverlay.MaterialComponents.ActionBar">
        <item name="android:background">#fff</item>
    </style>
like image 115
humazed Avatar answered Sep 21 '22 17:09

humazed