Material-components-android: setting background to Button has no effect

Created on 24 Aug 2020  路  5Comments  路  Source: material-components/material-components-android

after i fix my issue here #1649 and changing themr, my

bug

All 5 comments

@gabrielemariotti if u can help me

Check the doc.

Use theapp:backgroundTint attribute to apply a color to the button background.

I have this issue too and use of app:backgroudTint doesn't help.

I use Bridge theme for activity and style for button based on Widget.MaterialComponents.Button. It's work for library version 1.1.0 but it doesn't work for 1.2.0 and 1.2.1. If I change back button style to Widget.AppCompat.Button.Colored, it's work with 1.2.0 and 1.2.1 but new material styles have different font and it looks ugly to combine this styles.

Styles:

<!-- activity theme -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
    <item name="colorPrimary">#FFFFFF</item>
    <item name="colorPrimaryDark">#FFFFFF</item>
    <item name="colorAccent">#802095</item>
    <item name="android:screenOrientation">portrait</item>
    <item name="android:windowBackground">#FAFAFA</item>
</style>

<!-- button style -->
<style name="MainButton" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">#802095</item>
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textAllCaps">true</item>
</style>

Usage:

<Button
    style="@style/MainButton"
    android:id="@+id/button"
    android:layout_width="match_parrent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:text="test"/>

material-components-android 1.1.0
image

material-components-android 1.2.1
image

@fAntel
It is a different issue.

You can check in the Layout Inspector:

Schermata 2020-09-03 alle 13 59 27

In your case since you are using a Bridge Theme, you are working with an AppCompatButton.

You can use:

<com.google.android.material.button.MaterialButton
    style="@style/MainButton"
    android:id="@+id/button"
    android:layout_width="match_parrent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:text="test"/>

or

    <style name="AppThemeBridge" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
        <item name="viewInflaterClass">com.google.android.material.theme.MaterialComponentsViewInflater</item>
    </style>

otherwise use the AppCompatButton and theandroid:background attribute.

@gabrielemariotti thank you very much :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielwilson1702 picture danielwilson1702  路  3Comments

sepehr-alipour picture sepehr-alipour  路  3Comments

MrCreeper1008 picture MrCreeper1008  路  3Comments

JakeWharton picture JakeWharton  路  3Comments

KelvinPac picture KelvinPac  路  3Comments