Thank @Aleksey for the idea.
I add a little bit more code to make the UX more perfect.
XML:
app:icon="@drawable/ic_in"
android:text="@string/sign_in"
app:iconGravity="textStart"
app:iconSource="@{@drawable/ic_in}"
app:showProgress="@{isLoading}"
app:textSource="@{@string/sign_in}"
Binding Adapter:
@BindingAdapter(value = ["showProgress", "iconSource", "textSource"], requireAll = false)
fun MaterialButton.setShowProgress(
showProgress: Boolean?,
iconSource: Drawable?,
textSource: String?
) {
icon = if (showProgress == true) {
CircularProgressDrawable(context!!).apply {
setStyle(CircularProgressDrawable.LARGE)
setColorSchemeColors(ContextCompat.getColor(context!!, R.color.purple_200))
start()
}
} else iconSource
text = if (showProgress == true) "" else textSource
if (icon != null) { // callback to redraw button icon
icon.callback = object : Drawable.Callback {
override fun unscheduleDrawable(who: Drawable, what: Runnable) {
}
override fun invalidateDrawable(who: Drawable) {
[email protected]()
}
override fun scheduleDrawable(who: Drawable, what: Runnable, `when`: Long) {
}
}
}
}