In this Android kotlin example we will learn how to set the color to the Progressbar programmatically. Progressbar is a widget which will used to show the progress status of the work in the android application. we always set the colors of the widget inside xml files, but how we can set the color to the Progressbar dynamically.
There are two ways to set the color to the progressbar by programmatically
progressBar.progressTintList
progressBar2.progressDrawable.colorFilter
Set color by progressBar.progressTintList
progressBar.progressTintList= ColorStateList.valueOf(Color.GREEN)
|
set color by progressBar2.progressDrawable.colorFilter
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
progressBar2.progressDrawable.colorFilter =
BlendModeColorFilter(Color.BLUE, BlendMode.SRC_IN)
}else {
progressBar2.progressDrawable
.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN)
}
|
Let's create android example to set color for the progressbar dynamically
Step 1: Create Android application
Step 2: Update xml file with below code, this xml file contains two progressbar, one textview and one button
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/txt_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:textAppearance=
"@style/TextAppearance.MaterialComponents.Headline6"
android:text="0" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"/>
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Start"
android:textColor="#FFF"
android:backgroundTint="#000" />
</androidx.appcompat.widget.LinearLayoutCompat>
|
Step 3: Inflate views in activity screen
package com.rrtutors.kotlinexample2021
import android.content.res.ColorStateList
import android.graphics.BlendMode
import android.graphics.BlendModeColorFilter
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton
import com.google.android.material.textview.MaterialTextView
import java.lang.Thread.sleep
class ProgressbarActivity : AppCompatActivity() {
lateinit var txt_progress:MaterialTextView
lateinit var progressBar:ProgressBar
lateinit var progressBar2:ProgressBar
lateinit var btn_event:MaterialButton
var progres:Int=0;
lateinit var thread:Thread;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_progressbar)
txt_progress=findViewById(R.id.txt_progress);
progressBar=findViewById(R.id.progressBar);
progressBar2=findViewById(R.id.progressBar2);
btn_event=findViewById(R.id.btn_event);
progressBar.progress=10;
progressBar2.progress=10;
progressBar.progressTintList= ColorStateList.valueOf(Color.GREEN)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
progressBar2.progressDrawable.colorFilter =
BlendModeColorFilter(Color.BLUE, BlendMode.SRC_IN)
}else {
progressBar2.progressDrawable
.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN)
}
btn_event.setOnClickListener {
thread= Thread(Runnable {
while (progres < 100){
progres=progres+1;
Log.v("Progress","Progress "+progres);
runOnUiThread {
progressBar.progress = progres
progressBar2.progress = progres
txt_progress.text = "$progres %"
progressBar.progressTintList= ColorStateList.valueOf(Color.RED)
}
}
})
thread.start();
}
}
}
|
Step 4: Now run the application
Conclusion: In this example we learned how to inflate views from xml file and set color to the progressbar programmatically.
Circular seekbar android