Android Button Change Background Not Working on Material Theme

August 28, 2019

If you set App theme to material at res/values/styles

<resources>
    <!-- Original Parent: Theme.AppCompat.Light.DarkActionBar -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

Changing android:background wouldn’t work as Button is now com.google.android.material.button.MaterialButton.

<Button
    android:text="Test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    />

If you need it to behave like android.widget.Button instead of com.google.android.material.button.MaterialButton, use

<android.widget.Button
    android:text="Test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    />

You can also use

<androidx.appcompat.widget.AppCompatButton
    android:text="Test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    />
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.