Kotlin progressbar change color

How do i set the progressbar color dynamically android kotlin

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

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

Android Set progressbar color dynamically

Conclusion: In this example we learned how to inflate views from xml file and set color to the progressbar programmatically.

Источник

Android ProgressBar using Kotlin

Android ProgressBar using Kotlin

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

What is a ProgressBar?

ProgressBar UI element is used to display the Progress on the app screen. We can use a ProgressBar to show the download/upload progress on the app screen.

Progress Bar Types

  1. Determinate ProgressBar — This is used when you can track and show the progress completed.
  2. Indeterminate ProgressBar — This one goes infinitely until stopped.

A ProgressDialog would hold a ProgressBar inside an Alert Dialog. ProgressDialog is now deprecated since it isn’t a good idea to show long progress in a dialog while blocking the screen.

ProgressBar Attributes

Some of the important attributes of ProgressBar are:

  • android:indeterminate — used to specify the boolean value indicating the type of the ProgressBar
  • android:max — The upper limit of the progress
  • android:min — The lower limit of the progress
  • android:progress — The steps by which the progress would be incremented.
  • android:minWidth and minHeight — Used to define the dimensions of the ProgressBar
  • android:progressBarTint — The tint color of the progress completed of the ProgressBar
  • android:progressBarBackgroundTint — The tint color of the progress completed of the ProgressBar
  • style — Used to set the style of the ProgressBar. By default it is circular. We can set the style as @style/Widget.AppCompat.ProgressBar.Horizontal for the Horizontal ProgressBar
  • android:progressDrawable — Is used to set a drawable for the progress.
  • android:secondaryProgress — Indicates the secondary progress value. This is used when we want to show the sub-downloads/subtasks progress.

The default tint colors are set to the colorAccent defined in the styles.xml .

ProgressBar XML Layout

A basic circular indeterminate ProgressBar XML layout looks like this:

In the following section, we’ll implement various types of ProgressBars in our Android app using Kotlin.

Android ProgressBar Kotlin App Project Structure

android progressbar kotlin project structure

1. XML Layout Code

The code for the activity_main.xml layout is as follows.

In the last progress bar, we’ve set a progress drawable on the horizontal ProgressBar. The drawable.xml file is progress_states.xml .

In this drawable, we’ve created different states of the drawable. All are circular shaped and each layer would be displayed for the different states — idle, secondary progress, primary progress.

2. Kotlin Main Activity Code

Let’s look at the MainActivity.kt Kotlin class code.

package net.androidly.androidlyprogressbar import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.os.Handler import android.view.View import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() < var isStarted = false var progressStatus = 0 var handler: Handler? = null var secondaryHandler: Handler? = Handler() var primaryProgressStatus = 0 var secondaryProgressStatus = 0 override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) handler = Handler(Handler.Callback < if (isStarted) < progressStatus++ >progressBarHorizontal.progress = progressStatus textViewHorizontalProgress.text = "$/$" handler?.sendEmptyMessageDelayed(0, 100) true >) handler?.sendEmptyMessage(0) btnProgressBarSecondary.setOnClickListener < primaryProgressStatus = 0 secondaryProgressStatus = 0 Thread(Runnable < while (primaryProgressStatus < 100) < primaryProgressStatus += 1 try < Thread.sleep(1000) >catch (e: InterruptedException) < e.printStackTrace() >startSecondaryProgress() secondaryProgressStatus = 0 secondaryHandler?.post < progressBarSecondary.progress = primaryProgressStatus textViewPrimary.text = "Complete $primaryProgressStatus% of 100" if (primaryProgressStatus == 100) < textViewPrimary.text = "All tasks completed" >> > >).start() > > fun startSecondaryProgress() < Thread(Runnable < while (secondaryProgressStatus < 100) < secondaryProgressStatus += 1 try < Thread.sleep(10) >catch (e: InterruptedException) < e.printStackTrace() >secondaryHandler?.post < progressBarSecondary.setSecondaryProgress(secondaryProgressStatus) textViewSecondary.setText("Current task progress\n$secondaryProgressStatus% of 100") if (secondaryProgressStatus == 100) < textViewSecondary.setText("Single task complete.") >> > >).start() > fun horizontalDeterminate(view: View) < isStarted = !isStarted >> 

The horizontalDeterminate Kotlin function is triggered when the first button is clicked. It is used to start/stop Horizontal ProgressBar. A Handler is associated with a single thread. It is used to send messages to the Thread. The btnProgressBarSecondary click triggers the second progress bar. We have created two handlers — one for the normal progress and second for the subtasks. In each of them, we are setting the thread to sleep. For the secondary thread, the sleep time is 1/100 of the primary progress thread. The progress value is displayed on the TextView. Output:

You can download the project from the following link: AndroidlyProgressBar

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

Android ProgressBar using Kotlin

Android ProgressBar using Kotlin

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

What is a ProgressBar?

ProgressBar UI element is used to display the Progress on the app screen. We can use a ProgressBar to show the download/upload progress on the app screen.

Progress Bar Types

  1. Determinate ProgressBar — This is used when you can track and show the progress completed.
  2. Indeterminate ProgressBar — This one goes infinitely until stopped.

A ProgressDialog would hold a ProgressBar inside an Alert Dialog. ProgressDialog is now deprecated since it isn’t a good idea to show long progress in a dialog while blocking the screen.

ProgressBar Attributes

Some of the important attributes of ProgressBar are:

  • android:indeterminate — used to specify the boolean value indicating the type of the ProgressBar
  • android:max — The upper limit of the progress
  • android:min — The lower limit of the progress
  • android:progress — The steps by which the progress would be incremented.
  • android:minWidth and minHeight — Used to define the dimensions of the ProgressBar
  • android:progressBarTint — The tint color of the progress completed of the ProgressBar
  • android:progressBarBackgroundTint — The tint color of the progress completed of the ProgressBar
  • style — Used to set the style of the ProgressBar. By default it is circular. We can set the style as @style/Widget.AppCompat.ProgressBar.Horizontal for the Horizontal ProgressBar
  • android:progressDrawable — Is used to set a drawable for the progress.
  • android:secondaryProgress — Indicates the secondary progress value. This is used when we want to show the sub-downloads/subtasks progress.

The default tint colors are set to the colorAccent defined in the styles.xml .

ProgressBar XML Layout

A basic circular indeterminate ProgressBar XML layout looks like this:

In the following section, we’ll implement various types of ProgressBars in our Android app using Kotlin.

Android ProgressBar Kotlin App Project Structure

android progressbar kotlin project structure

1. XML Layout Code

The code for the activity_main.xml layout is as follows.

In the last progress bar, we’ve set a progress drawable on the horizontal ProgressBar. The drawable.xml file is progress_states.xml .

In this drawable, we’ve created different states of the drawable. All are circular shaped and each layer would be displayed for the different states — idle, secondary progress, primary progress.

2. Kotlin Main Activity Code

Let’s look at the MainActivity.kt Kotlin class code.

package net.androidly.androidlyprogressbar import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.os.Handler import android.view.View import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() < var isStarted = false var progressStatus = 0 var handler: Handler? = null var secondaryHandler: Handler? = Handler() var primaryProgressStatus = 0 var secondaryProgressStatus = 0 override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) handler = Handler(Handler.Callback < if (isStarted) < progressStatus++ >progressBarHorizontal.progress = progressStatus textViewHorizontalProgress.text = "$/$" handler?.sendEmptyMessageDelayed(0, 100) true >) handler?.sendEmptyMessage(0) btnProgressBarSecondary.setOnClickListener < primaryProgressStatus = 0 secondaryProgressStatus = 0 Thread(Runnable < while (primaryProgressStatus < 100) < primaryProgressStatus += 1 try < Thread.sleep(1000) >catch (e: InterruptedException) < e.printStackTrace() >startSecondaryProgress() secondaryProgressStatus = 0 secondaryHandler?.post < progressBarSecondary.progress = primaryProgressStatus textViewPrimary.text = "Complete $primaryProgressStatus% of 100" if (primaryProgressStatus == 100) < textViewPrimary.text = "All tasks completed" >> > >).start() > > fun startSecondaryProgress() < Thread(Runnable < while (secondaryProgressStatus < 100) < secondaryProgressStatus += 1 try < Thread.sleep(10) >catch (e: InterruptedException) < e.printStackTrace() >secondaryHandler?.post < progressBarSecondary.setSecondaryProgress(secondaryProgressStatus) textViewSecondary.setText("Current task progress\n$secondaryProgressStatus% of 100") if (secondaryProgressStatus == 100) < textViewSecondary.setText("Single task complete.") >> > >).start() > fun horizontalDeterminate(view: View) < isStarted = !isStarted >> 

The horizontalDeterminate Kotlin function is triggered when the first button is clicked. It is used to start/stop Horizontal ProgressBar. A Handler is associated with a single thread. It is used to send messages to the Thread. The btnProgressBarSecondary click triggers the second progress bar. We have created two handlers — one for the normal progress and second for the subtasks. In each of them, we are setting the thread to sleep. For the secondary thread, the sleep time is 1/100 of the primary progress thread. The progress value is displayed on the TextView. Output:

You can download the project from the following link: AndroidlyProgressBar

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

Читайте также:  Python генератор случайного числа
Оцените статью