Android ProgressBar Using Kotlin With Example

In this article, we will learn about android ProgressBar using Kotlin. We will go through various example that demonstrates how to use different attributes of ProgressBar. For example, …..

In this article, we will get answer to questions like –

  • What is ProgressBar?
  • Why should we consider ProgressBar while designing ui for any app?
  • What are possibilities using ProgressBar while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin ProgressBar Output Android ProgressBar Using Kotlin Tutorial With Example

Tutorialwing Kotlin ProgressBar Output

Getting Started

We can define android ProgressBar widget as below –

Android progressBar widget is an user interface that indicates the progress of an operation.

ProgressBar supports two modes of operation. They are –

  • Determinate ProgressBar
  • InDeterminate ProgressBar

Determinate ProgressBar is used when you want to show a fixed amount of progress i.e. 10%, 20%, 100% etc. For Example, When you to show progress of file download from server. It may be 10%, 20% or 100% . In such scenario, we use determinate progressBar.

InDeterminate ProgressBar is used when you want don’t know exact progress of an operation but you want to show that some operation is going on. In Other words, It is used when you do not know how long an operation will take to complete.

In this post, we will talk about indeterminate progressBar. However, you can customise indeterminate progressbar using it’s attributes to use it as determinate progressBar.

Now, how do we use ProgressBar in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as ProgressBar. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android ProgressBar Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using ProgressBar in Kotlin

Follow steps below to use ProgressBar in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">ProgressBar</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <ProgressBar
            android:id="@+id/progressBar_ID"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/button_ID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HIDE PROGRESSBAR"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/progressBar_ID" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    In activity_main.xml file, we have defined progressbar and button widget. Now, we will access button and progressbar using kotlin file to perform some operations.

  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.progressbar
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.View
    import com.tutorialwing.progressbar.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    
    		setupProgressBar()
    	}
    
    	private fun setupProgressBar() {
    		val progressBar = binding.progressBarID
    		binding.buttonID.setOnClickListener {
    			val visibility = if (progressBar.visibility == View.GONE) View.VISIBLE else View.GONE
    			progressBar.visibility = visibility
    
    			val btnText =
    				if (progressBar.visibility == View.GONE) "SHOW PROGRESSBAR" else "HIDE PROGRESSBAR"
    			binding.buttonID.text = btnText
    		}
    	}
    }
    

    In MainActivity.kt file, we have accessed progressBar and button widget. We have also set a click listener on button to show / hide progressBar.

Now, run the application. We will get output as below –

Tutorialwing Kotlin ProgressBar Output Android ProgressBar Using Kotlin Tutorial With Example

Tutorialwing Kotlin ProgressBar Output

Different Attributes of ProgressBar in XML

Now, we will see how to use different attributes of Android ProgressBar using Kotlin to customise it –

Set Id of ProgressBar

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of ProgressBar using android:id attribute like below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        />

Here, we have set id of ProgressBar as progressbar_ID using android:id=”” attribute. So, if we need to reference this ProgressBar, we need to use this id – progressbar_ID.
Learn to Set ID of ProgressBar Dynamically

Set Width of ProgressBar

We use android:layout_width=”” attribute to set width of ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of ProgressBar Dynamically

Set Height of ProgressBar

We use android:layout_height=”” attribute to set height of ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of ProgressBar Dynamically

Set Padding of ProgressBar

We use android:padding=”” attribute to set padding of ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in ProgressBar using android:padding=”” attribute.
Learn to Set Padding of ProgressBar Dynamically

Set Margin of ProgressBar

We use android:layout_margin=”” attribute to set margin of ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in ProgressBar using android:layout_margin=”” attribute.
Learn to Set Margin of ProgressBar Dynamically

Set Background of ProgressBar

We use android:background=”” attribute to set background of ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in ProgressBar using android:background=”” attribute.
Learn to Set Background of ProgressBar Dynamically

Set Visibility of ProgressBar

We use android:visibility=”” attribute to set visibility of ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of ProgressBar using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of ProgressBar Dynamically

Set Text of ProgressBar

We use android:text=”” attribute to set text of ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        />

Here, we have set text (“Hello Tutorialwing”) in ProgressBar using android:text=”” attribute.
Similarly, we can set any text using this attribute.
Learn to Set Text of ProgressBar Dynamically

Set Color of Text in ProgressBar

We use android:textColor=”” attribute to set color of text in ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textColor="#ffffff"     
        />

Here, we have set color (#ffffff i.e. white) of text (“Hello Tutorialwing”) in ProgressBar using android:textColor=”” attribute. Similarly, we can set any color using this attribute.
Learn to Set Color of ProgressBar Dynamically

Set Gravity of ProgressBar

We use android:gravity=”” attribute to set gravity of text in ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:gravity="center_horizontal"
        />

Here, we have set gravity of text in ProgressBar using android:gravity=”” attribute. Attribute value can be – “center_horizontal”, “center”, “center_vertical” etc.
Learn to Set Gravity of ProgressBar Dynamically

Set Text in Uppercase, Lowercase

If we need to show text of ProgressBar in uppercase or lowercase etc.

Set text in uppercase

We can use android:textAllCaps=”true” attribute to set text in uppercase. We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textAllCaps="true"
        />

Attribute android:textAllCaps=”true” sets text in uppercase. So, HELLO TUTORIALWING is set in ProgressBar.

By default, false is set in this attribute. So, Whatever value is written in android:text=”” attribute, it will be set as it is. For example,

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textAllCaps="false"
        />

Above code will set Hello Tutorialwing to ProgressBar.

How do we set text in lowercase?

Answer –

  • In xml file – write all the text in lowercase.
  • In kotlin file – take text as string. Then, convert it in lowercase. Then, set it to ProgressBar.

Learn to Set Text in Uppercase or Lowercase Dynamically

Set Size of Text in ProgressBar

We use android:textSize=”” attribute to set size of text in ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textSize="20sp"
        />

Here, we have set size of text in ProgressBar using android:textSize=”” attribute.
Learn to Set Size of Text of ProgressBar Dynamically

Set Style (Bold/italic) of Text in ProgressBar

We use android:textStyle=”” attribute to set style (bold, italic etc.) of text in ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textStyle="bold"
        />

Here, we have set style of text in ProgressBar using android:textStyle=”” attribute. This attribute can take bold, italic or normal.
Learn to Set Style of Text of ProgressBar Dynamically

Set Letter Spacing of Text in ProgressBar

We use android:letterSpacing=”” attribute to set spacing between letters of text in ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:letterSpacing="1"
        />

Here, we have set spacing between letters of text in ProgressBar using android:letterSpacing=”” attribute.
Learn to Set Letter Spacing of Text of ProgressBar Dynamically

Set Typeface of Text in ProgressBar

We use android:typeface=”” attribute to set typeface in ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:typeface="sans"
        />

Here, we have set typeface of text in ProgressBar using android:typeface=”” attribute. This attribute can take values – “sans”, “normal”, “monospace” or “normal”.
Learn to Set Typeface of ProgressBar Dynamically

Set fontFamily of Text in ProgressBar

We use android:fontFamily=”” attribute to set fontFamily of text in ProgressBar.
We can do it as below –

    <ProgressBar
        android:id="@+id/progressbar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:fontFamily="sans-serif"
        />

Here, we have set fontFamily (Here, sans-serif) of text in ProgressBar using android:fontFamily=”sans-serif” attribute.

Till now, we have see how to use android ProgressBar using Kotlin. We have also gone through different attributes of ProgressBar to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android ProgressBar Widget

Below are the various attributes that are used to customise android ProgressBar Widget. However, you can check the complete list of attributes of ProgressBar in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

Some of the popular attributes of Android ProgressBar Widget are –

Sr. XML Attributes Description
1 android:indeterminate It is used to set indeterminate mode of the progressbar
2 android:indeterminateDrawable It is used to set drawable of the progressbar for the indeterminate mode
3 android:indeterminateDuration It is used to set duration of the indeterminate animation
4 android:max It is used to set maximum value
5 android:min It is used to set minimum value
6 android:progress It is used to set default progress value (between 0 and max value)
7 android:progressDrawable It is used to set drawable used for the progress mode
8 android:secondaryProgress It is used to set secondary progress value (between 0 and max value)

Some of the popular attributes of Android ProgressBar inherited from View are –

Sr. XML Attributes Description
1 android:alpha It is used to set alpha of view
2 android:background It is used to set background drawable for the view.
3 android:clickable It is used to set if this view is clickable or not.
4 android:elevation It is used to set base z-depth of the view.
5 android:id It is used to set id of the view. Id must be unique.
6 android:onClick It is used to set action to perform when this view is clicked.
7 android:padding It is used to set padding of the view.
8 android:visibility It is used to set visibility of the view. For example, VISIBLE, INVISIBLE etc.

We have seen different attributes of ProgressBar and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is ProgressBar, how can we use android ProgressBar using Kotlin ? etc. We also went through different attributes of android ProgressBar.

Leave a Reply