This tutorial will answer following questions:
- Difference between android:background & android:backgroundTint in android
- Why background color is not applying to Views/Buttons in android
- Why android:background is not working in android?
- Why button background color is not changing ?
- Difference between app: and android: prefix, used in android XML layouts
android:background & android:backgroundTint are two very important and confusing properties. Both are used for background styling of a View/Layout/Button. Here, I am listing some important points about the functionality these properties/attributes.
- Both are used to change background color of a View/Button/Layout.
- android:background applies a color or a full drawable resource (PNG image, XML state list description) to the background, while android:backgroundTint is only used for coloring the background
- android:backgroundTint is having high priority than android:background i-e if you set both of these attributes/properties for a view, android:backgroundTint will affect/apply while android:background will not.
- If android:background attribute/property is used for changing background color, the default click effect behavior (view/button rounded corners and default selection states) of the view will be lost.
- use android:background for background resource(PNG/XML) setting and android:backgroundTint for color changing
- Use backgroundTint instead of background, until unless there is some special need (e-g wanna to change background image for a View).
- By default, Views/buttons/layouts usually use custom/support-library values (purple color +some padding) for backgroundTint property so if you wanna to change it by using/setting android:backgroundTint property, it will not work. It's because app:backgroundTint (default one) have high priority than android:backgroundTint. So to replicate the changes, you will have to do one of the following:
- Disable its custom values in the res->values->themes->themes.xml OR
- Use app:backgroundTint instead of android:backgroundTint