How to get Android's native progress bar indeterminate drawable?

Viewed 192

I was wondering if it was possible to retrieve Android's native ProgressBar's indeterminate drawable, so that it can used on another view.

Looking at ProgressBar's source code:

  1. the default attributes are located at com.android.internal.R.attr.progressBarStyle, which I believe is the same as android.R.attr.progressBarStyle (according to this answer).
  2. the resource id for the drawable I am looking for is R.styleable.ProgressBar_indeterminateDrawable

However, I have no luck accessing that styleable attribute itself.

Any idea on how to achieve such a thing?

1 Answers

This is how I do in my app. IIRC, this code was provided by Jake Wharton on the retrofit site for getting a progress bar while files are downloading.

fun getProgressBarIndeterminate(): Drawable? {
    val attrs = intArrayOf(android.R.attr.indeterminateDrawable)
    val attrsIndeterminateDrawableIndex = 0
    val a = App.getContext().obtainStyledAttributes(android.R.style.Widget_ProgressBar, attrs)
    return try {
        a.getDrawable(attrsIndeterminateDrawableIndex)
    } catch (e: Exception) {
        return null
    }finally {
        a.recycle()
    }
}
Related