Android development is the fastest-growing field. Nowawad, more people work from home, and in order to work with full efficiency, the employees need all the resources on their devices, that’s where mobile applications come handy.

In the coming years, mobile app development is going to be a separate department in every company. You can have a bright future in this field. That is the reason we have started this series, where we help beginners to learn mobile app development.

In this tutorial, you will learn how to create a progress bar in an android application using Kotlin.

Progress Bar using Kotlin

Step 1. Create a new Android Project with whatever name you want.

Step 2. Add a progress bar attribute.

In the XML file, i.e., Main Activity, you need to add a progress bar attribute. There are two different ways to add this attribute.

First, you can write the code manually, or secondly, you can drag and drop. For this tutorial, we are going to follow the first method.

Copy the following code in the activity_main.xml file:

<ProgressBar
android:id="@+id/progressbar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="200dp"
android:layout_height="200dp"
android:padding="0dp"
android:layout_centerInParent=“true"/>

Apart from this progress bar, we are going to use a button.

You can code the whole code (attached below) and can replace the code of acitivity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
    <ProgressBar
android:id="@+id/progressbar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="200dp"
android:layout_height="200dp"
android:padding="0dp"
android:layout_centerInParent="true"/>
    <Button
android:id="@+id/Button"
android:layout_below="@id/progressbar"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Click Here"
android:layout_centerInParent="true"
android:visibility="visible" tools:visibility="visible"/>
</RelativeLayout>

Step 3. Now, you need to open the MainActivity.kt to program the progress bar that we have created in Step 2.

Copy the following code and paste it in MainActivity.kt :

import android.animation.ObjectAnimator
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

    class MainActivity: AppCompatActivity() {

        override fun onCreate(savedInstanceState: Bundle ? ) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            val Button = findViewById < Button > (R.id.Button)

            progressbar.max = 100

            val currentProgress = 100

            ObjectAnimator.ofInt(progressbar, "progress", currentProgress)
                .setDuration(2000)
                .start()

            Button ? .setOnClickListener() {

                progressbar.visibility = View.GONE
                Button.visibility = View.GONE
                Toast.makeText(this, "ProgressBar is gone", Toast.LENGTH_LONG).show()
                val intent: Intent = Intent(this, first::class.java)
                startActivity(intent)
                finish()
            }
        }
    }

Here a button is used just for the illustration purpose. You can avoid using it as it is not a part of the progress bar.

Here a few values are optional, and you can change it as per your requirements, such as:

setDuration(2000), where 2000 is the time in milliseconds that sets the duration of the progress bar.

progressbar.max, the value of this attribute can be changed as it is the total progress that you want your progress bar to make.

currentProgress, this value determines the number of intervals; the progress bar will be updated.

Step 4. Build the application and deploy it.

You will get an interface something like this:

Final Notes

That’s how you create a progress bar in your mobile application using kotlin. If you are facing any issues, you can drop down your query with the error message.

Last modified: August 31, 2020

Author

Comments

Write a Reply or Comment

Your email address will not be published.