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.
<? 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> |
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)
package com.w3wiki.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 } } } |
AndroidManifest.xml file
<?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package = "com.w3wiki.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> |