I would like to change the progress bar to a custom drawable. How do I change the image of the progress bar?
Android ProgressBar is a graphical view indicator that shows some progress. Android progress bar displays a bar representing the completing of the task. Progress bar in android is useful since it gives the user an idea of time to finish its task.
In Android, by default a progress bar will be displayed as a spinning wheel but If we want it to be displayed as a horizontal bar then we need to use style attribute as horizontal. It mainly use the “android. widget. ProgressBar” class.
Here my answer, i use my image in android progress bar..
 <ProgressBar         android:layout_width="60dp"         android:layout_height="50dp"         android:layout_centerInParent="true"         android:indeterminate="true"         android:indeterminateDrawable="@drawable/my_progress_indeterminate" />   my_progress_indeterminate.xml:
  <?xml version="1.0" encoding="utf-8"?>     <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"          android:drawable="@drawable/animation"          android:pivotX="50%"         android:pivotY="50%"/>   animation.xml:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false">      <item android:drawable="@drawable/load" android:duration="50" />     <item android:drawable="@drawable/load" android:duration="50" />     <item android:drawable="@drawable/load" android:duration="50" />      <rotate           xmlns:android="http://schemas.android.com/apk/res/android"           android:drawable="@drawable/load"           android:pivotX="50%"           android:pivotY="50%"           android:fromDegrees="330"           android:toDegrees="360"           android:repeatCount="1" />       </animation-list> 
                        You can create a class by extending ImageView and use it like following in a similar way to ProgressBar with a rotation animation.
class CustomProgressBar : AppCompatImageView {
constructor(context: Context) : super(context)
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr)
private var anim: RotateAnimation? = null
init {
    setImageResource(R.drawable.your_custom_drawable)
    anim = RotateAnimation(
        0f, 350f, Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f
    )
    anim!!.interpolator = LinearInterpolator()
    anim!!.repeatCount = Animation.INFINITE
    anim!!.duration = 1000
    startAnimation(anim)
}
fun getRotateAnimation(): RotateAnimation? {
    return anim
}}
And in your layout.xml just use this class as your ProgressBar
            <com.example.CustomProgressBar
            android:id="@+id/customProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />
And in your fragment or activity show or hide it like following:
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    customProgressBar = findViewById(R.id.customProgressBar)
}
fun showCustomProgressBar() {
    customProgressBar.visibility = VISIBLE
    customProgressBar.startAnimation(customProgressBar.getRotateAnimation())
}
fun hideCustomProgressBar() {
    customProgressBar.visibility = GONE
    customProgressBar.clearAnimation()
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With