Android Button Change Background Not Working on Material Theme

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"
    />

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.