Android ProgressBar Using Kotlin With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this post, we are going to learn about how to use android progressbar using kotlin. We will also learn about different attributes that can be used to customise this progressbar widget.

Output

Tutorialwing Kotlin ProgressBar Output Android ProgressBar Using Kotlin Tutorial With Example

Tutorialwing Kotlin ProgressBar Output

Getting Started

Android ProgressBar can be defined as below –

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

ProgressBar supports two modes of operation. They are –
a. Determinate ProgressBar
b. 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.

Different Attributes of Android ProgressBar 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.



Example of Android ProgressBar Using Kotlin

Now, we will create an android application and use progressBar widget in it.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ProgressBar. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Now, we will modify xml and kotlin file to use progressbar using kotlin in application.

2. Modify values folder

No values folders have been modified. So, we are not going to mention them here.

3. Use ProgressBar Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="vertical">

	<ProgressBar
		android:id="@+id/progressBar"
		style="?android:attr/progressBarStyle"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>

	<Button
		android:id="@+id/button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Hide Progressbar"/>

</LinearLayout>

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.

4. Access ProgressBar Widget in Kotlin file

Open src/main/java/com.tutorialwing.progressbar/MainActivity.kt file and add below code into it.

package com.tutorialwing.progressbar

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.ProgressBar

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val progressBar = findViewById<ProgressBar>(R.id.progressBar)
        if (progressBar != null) {
            val btn = findViewById<Button>(R.id.button)
            btn?.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"
                btn.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.

Since AndroidManifest.xml file is very important in any android application. We are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.progressbar"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the program, we will get output as shown above.

That’s end of our tutorial on Android progressbar using Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners