This project demonstrates a bug in MaterialButton
(a widget in Material Components for Android).
Issue: material-components/material-components-android#982
Calling View.getDrawableState
on a MaterialButton
within an OnAttachStateChangeListener
computes a new drawable state without notifying drawableStateChanged
. This results in the
MaterialButton
failing to update the drawable state of the background tint until the next time it
changes (by e.g. touching the button, etc). getDrawableState
is used by many APIs and something as
simple as setTextColor
will call getDrawableState
and expose the problem.
In the following demo, we attach a fragment (defined in MainFragment
)
at some point after the activity (defined in MainActivity
) is created, and
while the window has focus. The background tint of the button (defined in button_tint_selector
)
defines the disabled state as red with a default state of green.
The background tint incorrectly displays the disabled state even though the button is in the enabled state. Touching the button (or un-focusing and re-focusing the window, etc) synchronizes the state to the background tint and the issue resolves itself.