Dynamic ProgressBar in Kotlin - GeeksforGeeks

23 Feb, 2021
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.
Second step is to design our layout page. Here, we will use the RelativeLayout to get the ProgressBar from the Kotlin file.
<?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>
Update the strings.xml file
<resources>    <string name="app_name">DynamicProgressBarInKotlin</string></resources>
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)

package com.geeksforgeeks.myfirstkotlinapp  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.Viewimport android.view.ViewGroupimport android.widget.Buttonimport android.widget.LinearLayoutimport android.widget.ProgressBarimport 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        }      }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"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>
shubham_singh
Android-Bars
Kotlin Android
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Android RecyclerView in Kotlin
Services in Android with Example
CardView in Android With Example
Broadcast Receiver in Android With Example
Android RecyclerView in Kotlin
Android UI Layouts
Services in Android with Example
Content Providers in Android with Example