I've created a custom view that extends MaterialButton that replaces the drawable with a CircularProgressIndicator when it's loading.
However when I replace the drawable with the CircularProgressIndicator the iconGravity doesn't work anymore. I don't see what I'm doing wrong.
I've been able to boil the class down to this:
class LoadingButton constructor(context: Context) : MaterialButton(context) {
private val progressIndicatorDrawable by lazy {
val progressIndicatorSpec = CircularProgressIndicatorSpec(context, null, 0, R.style.Widget_MaterialComponents_CircularProgressIndicator_ExtraSmall)
progressIndicatorSpec.indicatorColors = intArrayOf(getColor(context, R.color.white))
IndeterminateDrawable.createCircularDrawable(context, progressIndicatorSpec).apply {
setVisible(true, true)
}
}
init {
// the following drawable is aligned to the start of the view (incorrect).
icon = progressIndicatorDrawable
// the following drawable is aligned to the start of the text (correct).
// icon = DrawableUtil.getDrawable(context, R.drawable.icon_delete)
text = "Delete"
iconGravity = ICON_GRAVITY_TEXT_START
}
}
I'm using this dependency
// I've also tried 1.5.0-alpha02
implementation "com.google.android.material:material:1.3.0"
Correct position The drawable is positioned at the start of the text.
Incorrect position The progress indicator is not positioned at the start of the text anymore.
It is curious that one drawable works but another doesn't. It may be worth reporting as a bug. In the meantime, I can offer a workaround that doesn't involve a lot of work (IMO).
If we take your custom view and remove the
iconGravity
and set the gravity for the text to the start and vertical center:we can get the indeterminate and the text together on the left side of the button:
The issue now is how to get the pair centered in the button. We can take a hint from the code for MaterialButton on an approach on how much space is needed on the left to center the drawable and text.
Putting it all together:
Here is the demo code. It may require some tweaking if your actual button is more complex than is pictured in your question.