Define toolbar gradient in styles.xml

I'm trying to define a gradient background for a toolbar, but I get a weird effect where the gradient becomes the background of all elements inside the toolbar:

toolbar propagating background

Other answers suggest setting the background in the layout, but I'm interested in getting it to work at the style level. Yes, an offer? Thanks in advance!

My defined styles are as follows:

   <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
      <!-- This puts the status bar translucent -->
      <item name="android:windowDrawsSystemBarBackgrounds">true</item>
      <item name="android:windowTranslucentStatus">true</item>

      <!-- set actionBar style. This att is referenced from layout to set the style-->
      <item name="android:actionBarStyle">@style/AppThemeToolBar</item>

   <style name="AppThemeToolBar" parent="@style/Theme.AppCompat">
        <item name="android:icon">@drawable/ic_logo</item>
        <item name="android:background">@drawable/toolbar_background</item>
        <!-- trying to set a text without background but is being ignored -->
        <item name="android:titleTextStyle">@style/ToolBarTitle</item>
        <!-- also ignored -->
        <item name="android:titleTextAppearance">@style/ToolBarTitle</style>


And this is the toolbar declaration in my xml layout:

     <!-- here actionBarStyle att is read -->



source to share

1 answer

The solution is as simple / silly as using the toolbarStyle att in styles.xml:

    <style name="MyTheme">
       <item name="toolbarStyle">@style/NeoToolbarStyle</item>
       <item name="toolbarNavigationButtonStyle">@style/ToolbarNavigationButtonStyle</item>


And of course my toolbar style can be used as usual with android: background without getting weird effect:

<style name="AppThemeToolBar" parent="@style/Widget.AppCompat.Toolbar">
    <item name="android:background">@drawable/my_beautiful_gradient</item>




All Articles