Android Button Disabled Color Not Working After Color Change

Apr 17, 2018
Use setAlpha

I believe the best way to change android button color is using backgroundTint. It maintain animation and effect of button when clicked, while backgroundTint, while [background]https://developer.android.com/reference/android/view/View.html#attr_android:background override the entire drawable (loosing default animation/effects/state color).

Change background color programatically using AppCompat support library (support up to API 4).

ViewCompat.setBackgroundTintList(button, ContextCompat.getColorStateList(this, android.R.color.white))

When you set the disable the button (button.isEnabled = false), the color doesn't change to reflect the disabled state.

There are a few solutions available, but my favourite is View.setAlpha.

button.alpha = if (button.isEnabled) 1f else 0.5f

❤️ 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.