urSus
urSus

Reputation: 12759

Material design components button global theme overriding broken

I want to simply apply styling to all buttons on a theme level like this

<style name="BaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
     ...    
    <item name="buttonStyle">@style/DefaultButton</item>
</style>

<style name="DefaultButton" parent="Widget.MaterialComponents.Button">
    <item name="android:textColor">@color/whatever</item>
</style>

<Button
    android:id="@+id/addChannelButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    android:layout_margin="16dp"
    android:text="Add room" />

Why doesnt this work? It would in appcompat

// If I use Theme.MaterialComponents.Light.NoActionBar.Bridge, then it works

Upvotes: 3

Views: 3642

Answers (4)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 365148

Use a Theme.MaterialComponents theme defining the materialButtonStyle attribute.

  <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ....

    <item name="materialButtonStyle">@style/MyButtonTheme</item>
  </style>

In this way you can customize the theme of all the buttons in your app.
Also starting from version 1.1.0 of the material library you can override the theme attributes from the default style using the materialThemeOverlay attribute.

Something like:

  <style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
    <item name="materialThemeOverlay">@style/ButtonStyleTextColor</item>
  </style>

  <style name="ButtonStyleTextColor">
    <!-- For filled buttons, your theme's colorPrimary provides the default background color of the component, and -->
    <!--the text color is colorOnPrimary -->
    <item name="colorOnPrimary">@color/my_color</item>

  </style>

Currently it requires version 1.1.0 of material components for android library.

Upvotes: 2

hamthenormal
hamthenormal

Reputation: 921

Use materialButtonStyle and use MaterialButton instead of Button

Upvotes: 0

Eugen Pechanec
Eugen Pechanec

Reputation: 38243

Can you try with v1.0.0-alpha06? There has been some progress since v1.0.0 which may have addressed what you're seeing.

1.1.0-alpha02

  • Shape Theming: FloatingActionButton, MaterialButton, Chip, MaterialCardView, BottomSheet, & TextInputLayout updated to use the new Material shape system

1.1.0-alpha06

  • Implement Shapeable interface in MaterialButton

Source: https://github.com/material-components/material-components-android/releases

Upvotes: 0

Cameron Ketcham
Cameron Ketcham

Reputation: 8006

You should be setting materialButtonStyle instead of buttonStyle in your theme.

Upvotes: 8

Related Questions