Dynamic ProgressBar in Kotlin

Android ProgressBar is user interface control that is used to show some kind of progress. For instance, loading of some page, downloading of some file or waiting for some event to complete.

In this article we will be discussing how to programmatically create a progress bar in Kotlin .

Firstly, we need to create a project in Android Studio. To do follow these steps:



  • Click on File, then New and then New Project and give name whatever you like
  • Then, select Kotlin language Support and click next button.
  • Select minimum SDK, whatever you need.
  • Select Empty activity and then click finish.

Modify activity_main.xml file

Second step is to design our layout page. Here, we will use the RelativeLayout to get the ProgressBar from the Kotlin file.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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">
  
  
    <RelativeLayout
            android:id="@+id/layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerHorizontal="true"
            android:layout_above="@+id/button">
  
    </RelativeLayout>
  
    <Button
            android:id="@+id/button"
            android:layout_marginTop="100dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Stop Loading"/>
  
</RelativeLayout>

chevron_right


Update the strings.xml file

filter_none

edit
close

play_arrow

link
brightness_4
code

<resources>
    <string name="app_name">DynamicProgressBarInKotlin</string>
</resources>

chevron_right


Create ProgressBar in MainActivity.kt file

Open app/src/main/java/yourPackageName/MainActivity.kt. In this file , we declare a variable progressBar to create the ProgressBar widget like this

       val progressBar = ProgressBar(this)
       //setting height and width of progressBar
       progressBar.layoutParams = LinearLayout.LayoutParams(
           ViewGroup.LayoutParams.WRAP_CONTENT,
           ViewGroup.LayoutParams.WRAP_CONTENT)

then add the widget in layout using this

val layout = findViewById(R.id.layout)
     // Add ProgressBar to our layout
     layout?.addView(progressBar)
filter_none

edit
close

play_arrow

link
brightness_4
code

package com.geeksforgeeks.myfirstkotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.RelativeLayout
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
  
        val progressBar = ProgressBar(this)
        //setting height and width of progressBar
        progressBar.layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
  
        //accessing our relative layout where the progressBar will add up
        val layout = findViewById<RelativeLayout>(R.id.layout)
        // Add ProgressBar to our layout
        layout?.addView(progressBar)
  
        //accessing the button which will handle the events,
        // whether to show progressBar or not
        val button = findViewById<Button>(R.id.button)
  
        //set a click listener to show/hide progressBar added in RelativeLayout.
        button?.setOnClickListener {
            val visibility = if (progressBar.visibility == View.GONE){
                View.VISIBLE
            }else
                View.GONE
            progressBar.visibility = visibility
  
            //setting button text
            //if we click "stop loading" button, text of button will change
            // to "start loading.." and vice versa
            val btnText = if (progressBar.visibility == View.GONE)
                "START LOADING..."
            else
                "STOP LOADING"
            button.text = btnText
        }
  
    }
}

chevron_right


AndroidManifest.xml file

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
package="com.geeksforgeeks.myfirstkotlinapp">
  
<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>

chevron_right


Run as Emulator:


 



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.