ProgressBar in Android:
ProgressBar is a user interface and it is used to display the progress of operations, such as Upload files.ProgressBar is used to display the status of the running process, for example: Analyze the status of the process or download files. On Android, the progress bar is displayed as a spinning wheel by default, but if we want it to be displayed as a horizontal bar, we must use the style attribute as the level.
There are two types of ProgressBar :
- Determinate ProgressBar
- Indeterminate ProgressBar
Determinate ProgressBar
This ProgressBar shows the current progress of reaching a certain maximum value.
<ProgressBar android:id="@+id/ProgressBar" style="?android:attr/progressBarStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" />
Indeterminate ProgressBar
In the undefined mode, the actual progress is not displayed, only a loop animation is displayed to indicate that some progress is taking place, as shown in the progress bar when the image is loaded in the image above.
<ProgressBar android:id="@+id/ProgressBar" style="?android:attr/progressBarStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminate="true"/>/////////////////////---------->indeterminate->true
Different attributes of ProgressBar :
XML Attribute | About |
---|---|
android: id | It is used to identify the control or view uniquely. |
android: progress | It is used to set the default progress value from 0 and max. It must be an integer value. |
android: indeterminate | it is used to enable indeterminate progress mode. |
android: background | it is used to set the background color for the progress bar. |
android: max | it is used to set the maximum value |
android: min | it is used to set the minimum value |
Preview of ProgressBar:
File: MainActivity.kt
–>In MainActivity.kt file initialize variable and implement progressbar.
package com.sagar.progressbar import android.os.Bundle import android.os.Handler import android.view.View import android.widget.Button import android.widget.ProgressBar import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private var ProgressBar: ProgressBar? = null private var i = 0 private var TextView: TextView? = null private val hdlr = Handler() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ProgressBar = findViewById<View>(R.id.ProgressBar) as ProgressBar TextView = findViewById<View>(R.id.ProTView) as TextView val btn = findViewById<View>(R.id.btnProBar) as Button btn.setOnClickListener { i = ProgressBar!!.progress Thread { while (i < 100) { i += 1 hdlr.post { ProgressBar!!.progress = i TextView!!.text = i.toString() + "/" + ProgressBar!!.max } try { Thread.sleep(500) } catch (e: InterruptedException) { e.printStackTrace() } } }.start() } } }
File: activity_main.xml
–>activity_main.xml contain progressbar and textview,button.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <ProgressBar android:id="@+id/ProgressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="142dp" android:indeterminate="false" android:max="100" android:minWidth="200dp" android:minHeight="50dp" android:progress="0" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/ProTView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="145dp" app:layout_constraintBottom_toTopOf="@+id/btnProBar" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/ProgressBar" /> <Button android:id="@+id/btnProBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="173dp" android:text="ProgressBar DEMO" android:textAllCaps="false" android:textColor="@color/black" android:backgroundTint="@color/teal_700" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
How to use ProgressBar in Android
Pingback: Adding Seekbar to Your App with Example - Developers Dome
Pingback: Using swipe-to-refresh layout in Android - Developers Dome
Pingback: Video Player Tutorial in Android Studio - Developers Dome
Pingback: Material Dialogs in Android with Example - Developers Dome
Pingback: Android Ratingbar Example | Kotlin - Developers Dome