ProgressBar in Kotlin

01 Sep, 2021
Android ProgressBar is a user interface control that indicates the progress of an operation. For example, downloading a file, uploading a file on the internet we can see the progress bar to estimate the time remaining in operation. There are two modes of ProgressBar: 
Determinate ProgressBar
Indeterminate ProgressBar
In common, we use the Determinate progress mode in ProgressBar because it shows the quantity of progress that has occurred like the (%) percentage of the file downloaded, how much data was uploaded or downloaded on the internet, etc. If we have to use determinate we set the style of the progress bar as below: 
XML
<ProgressBar    android:id="@+id/pBar"    style="?android:attr/progressBarStyleHorizontal"    android:layout_width="wrap_content"    android:layout_height="wrap_content" />
Here, we don’t get the idea of the progress of work means how much it has been completed or How long it will take to complete. We can use indeterminate progressBar like below by setting the indeterminate attribute as true. 
XML
<ProgressBar    android:id="@+id/pBar"    style="?android:attr/progressBarStyleHorizontal"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:indeterminate="true"/>
XML Attributes
Description
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">     <!-- adding progress bar -->    <ProgressBar        android:id="@+id/progress_Bar"        style="?android:attr/progressBarStyle"        android:layout_width="200dp"        android:layout_height="wrap_content"        android:layout_marginLeft="70dp"        android:layout_marginTop="150dp"        android:indeterminate = "true"        android:max="100"        android:minWidth="200dp"        android:minHeight="50dp"        android:visibility="invisible"        android:layout_centerInParent="true"        android:progress="0"        android:layout_marginStart="70dp" />       <!-- adding textview which will show the progress -->    <TextView        android:id="@+id/text_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/progress_Bar"        android:layout_centerHorizontal="true" />       <!-- adding button to start the progress -->    <Button        android:id="@+id/show_button"        android:layout_width="191dp"        android:layout_height="wrap_content"        android:layout_below="@+id/text_view"        android:layout_marginLeft="70dp"        android:layout_marginTop="20dp"        android:text="Progress Bar"        android:layout_centerHorizontal="true"        android:layout_marginStart="70dp" /> </RelativeLayout>
Kotlin
import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Buttonimport android.widget.ProgressBarimport android.widget.TextViewimport android.os.Handler class MainActivity : AppCompatActivity() {     private var progressBar: ProgressBar? = null    private var i = 0    private var txtView: TextView? = null    private val handler = Handler()         override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)                 // finding progressbar by its id        progressBar = findViewById<ProgressBar>(R.id.progress_Bar) as ProgressBar               // finding textview by its id        txtView = findViewById<TextView>(R.id.text_view)                 // finding button by its id        val btn = findViewById<Button>(R.id.show_button)                 // handling click on button        btn.setOnClickListener {            // Before clicking the button the progress bar will invisible            // so we have to change the visibility of the progress bar to visible            // setting the progressbar visibility to visible            progressBar.visibility = View.VISIBLE                         i = progressBar.progress                         Thread(Runnable {                // this loop will run until the value of i becomes 99                while (i < 100) {                    i += 1                    // Update the progress bar and display the current value                    handler.post(Runnable {                        progressBar.progress = i                                               // setting current progress to the textview                        txtView!!.text = i.toString() + "/" + progressBar.max                    })                    try {                        Thread.sleep(100)                    } catch (e: InterruptedException) {                        e.printStackTrace()                    }                }                                 // setting the visibility of the progressbar to invisible                // or you can use View.GONE instead of invisible                // View.GONE will remove the progressbar                progressBar.visibility = View.INVISIBLE                           }).start()        }    }}
XML
<?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>
Pushpender007
ruhelaa48
Android-Bars
Kotlin Android
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.