Tuesday, 7 September 2021

Android iconGravity not working for MaterialButton with CircularProgressIndicator

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. drawable correct position

Incorrect position The progress indicator is not positioned at the start of the text anymore. progress indicator incorrect position



from Android iconGravity not working for MaterialButton with CircularProgressIndicator

No comments:

Post a Comment