Change progressbar color through CODE ONLY in Android

前端 未结 12 1820
北海茫月
北海茫月 2020-12-01 01:12

I have a progressBar using the ProgressBar class.

Just doing this:

progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizont         


        
12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 02:02

    setColorFilter with 2 arguments is deprecated and the other answer pointing to use LightingColorFilter neither worked for me so

    val progressBar = ProgressBar(context, null, android.R.attr.progressBarStyle).apply {
    
        val colorFilter = PorterDuffColorFilter(
            ContextCompat.getColor(context, R.color.yourColor),
            PorterDuff.Mode.MULTIPLY
        )
    
        indeterminateDrawable.colorFilter = colorFilter
    }
    

    That will programmatically give you the circular progress bar with your color

提交回复
热议问题