結論
下記のようにContext
にExtensionを生やして、システムデフォルトのローディングインジケーターを取得することができます。drawable
の生成だけだと、アニメーション化されないため、(drawable as? Animatable)?.start()
でアニメーションを開始させています。
fun Context.getProgressBarDrawable(): Drawable { val value = TypedValue() theme.resolveAttribute(android.R.attr.progressBarStyleSmall, value, false) val progressBarStyle = value.data val attributes = intArrayOf(android.R.attr.indeterminateDrawable) val array = obtainStyledAttributes(progressBarStyle, attributes) val drawable = array.getDrawableOrThrow(0) array.recycle() (drawable as? Animatable)?.start() return drawable }
使い方はこう。
textInputLayout.endIconMode = TextInputLayout.END_ICON_CUSTOM textInputLayout.endIconDrawable = context?.getProgressBarDrawable()