How to create progress bar in android without using progress bar widget in xml file?

Viewed 272

I want to do something new in my app. I want to create Progress Bar without using tag of xml in android .is there any way to do that ? if yes then please share with me? The purpose of doing this is that i don't want to declare Progress Bar for each of my layout file. I completely want to make it using Kotlin Code in android. thanks in advnance

1 Answers

You can use the ProgressIndicator provided by the Material Components Library.

For example a CircularProgressIndicator:

    val layout = findViewById<LinearLayout>(R.id.layout)
    val indicator = CircularProgressIndicator(this)
    indicator.isIndeterminate = true
    indicator.trackColor = ContextCompat.getColor(this,R.color.xxx)
    indicator.indicatorSize = resources.getDimensionPixelSize(R.dimen.indicator100dp)
    indicator.setIndicatorColor(ContextCompat.getColor(this,R.color.xxx))
    indicator.show()
    layout.addView(indicator)

enter image description here

Or a LinearProgressIndicator

    val indicator = LinearProgressIndicator(this)
    indicator.isIndeterminate = true
    indicator.trackColor = ContextCompat.getColor(this,R.color.xxxx)
    indicator.trackThickness = resources.getDimensionPixelSize(R.dimen.cornerSize8)
    indicator.setIndicatorColor(ContextCompat.getColor(this,R.color.xxx))
    indicator.show()
    layout.addView(indicator)

enter image description here

Related