Search code examples
androidandroid-actionbarandroid-themeandroid-styles

Action bar is showing when using NoActionBar theme


I am using a theme with NoActionBar. Manifest file:

android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"

I am using another Action bar, which I include to the layout:

<include layout="@layout/toolbar" />

However, I always get two Toolbars, and the upper one is the one, which would be there if I did not use NoActionBar theme. I found out, that if I delete this part in styles.xml file, it works as it should - there is no second action bar:

<style name="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

Of course I would like to keep this in the style.xml file as this helps to set up also the colour of the notification panel.


Solution

  • It seems to me this fails because Theme.MaterialComponents.Light.NoActionBar is an existant theme and you are creating it in the styles.xml so it overrides the existant one.

    What I do is to create a new theme setting the one I want to edit as the parent theme:

    <style name="AppNoActionBar" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    </style>
    

    And then set this new theme on the manifest:

    android:theme="@style/AppNoActionBar"
    

    This way you'll have the properties of the base theme (Theme.MaterialComponents.Light.NoActionBar) with the custom updates.