I am in the process of replacing the buttons in my app with Material Buttons using
In my opinion, the best choice is to create a separate own style to the material button where it extends the material theme:
<style name="MatButton" parent="Theme.MaterialComponents">
<item name="colorOnPrimary">@android:color/holo_red_light</item>
<item name="colorPrimary">@android:color/holo_orange_dark</item>
<item name="colorOnSurface">@android:color/holo_orange_light</item>
</style>
<com.google.android.material.button.MaterialButton
android:id="@+id/searchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:textColor="@android:color/white"
android:textAppearance="@style/TextAppearance.MaterialComponents.Button"
android:theme="@style/MatButton"
/>
make changes in your styles.xml from before to after ->
before:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
after:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
As it suggest HERE you have to add a dependency in your build.gradle:
implementation 'com.google.android.material:material:1.0.0-beta01'
Or if you already use google support design library you must change your app theme to inherit from a Material Components theme
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
<!-- ... -->
If you cannot change your theme to inherit from a Material Components theme, you can inherit from a Material Components Bridge theme.
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
<!-- ... -->
How Reyske said:
note that it is out of beta now! So you can use implementation 'com.google.android.material:material:1.0.0'
When crashing The following message was output to Logcat.
Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
Therefore, you can also display MaterialButton by adding TextAppearance.
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/ok"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
You should use textAppearance
and your font style instead of fontFamily
& textSize
Try to update your app theme to inherit from Theme.MaterialComponents
(or a descendant)