Android ProgressBar Kotlin

15-Sep-2024

Learn how to use ProgressBar in Android Studio project Kotlin

Below is a simple example of how you can implement a ProgressBar in Android using Java. In this example, we'll create a basic Android application with a ProgressBar and a Button. When you click the button, the progress bar will start progressing



Here is the tutorial for Java




There Are Some Common Attributes for ProgressBar
:


id
This ID serves as the control's unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
progress
set Value for default progress


Step 1:

Create a new XML drawable resource:

Create a new XML file in the res/drawable directory. Let's name it progress_background.xml.


<!-- res/drawable/progress_background.xml -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Background shape -->
<item>
<shape>
<solid android:color="#E2918C8C"/> <!-- Background color -->
<corners android:radius="8dp"/> <!-- Corner radius -->
</shape>
</item>

<!-- Progress shape -->
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#1D201D"/> <!-- Progress color -->
<corners android:radius="8dp"/> <!-- Corner radius (same as background) -->
</shape>
</clip>
</item>

</layer-list>



Step 2:


File open res/layout/activity_main.xml file −:



<?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">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">


<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ProgressBar
android:id="@+id/simpleProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/buttonStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Progress" />
</LinearLayout>



</RelativeLayout>


Step 3:

File open com.microappvalley.newtestproject/MainActivity.kt file −


Kotlin:


package com.microappvalley.newtestproject

import android.annotation.SuppressLint
import android.opengl.Visibility

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.View.GONE
import android.widget.Button

import android.widget.ProgressBar

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
private lateinit var progressBar: ProgressBar
private lateinit var simpleProgressBar: ProgressBar
private lateinit var buttonStart: Button

private var progressStatus = 0
private val handler = Handler(Looper.getMainLooper())
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Find the ProgressBar in the layout
progressBar = findViewById(R.id.progressBar)
simpleProgressBar = findViewById(R.id.simpleProgressBar)
buttonStart = findViewById(R.id.buttonStart)
progressBar.visibility=View.GONE


// Start a thread to update the progres
buttonStart.setOnClickListener {
progressBar.visibility=View.VISIBLE
Thread {
while (progressStatus < 100) {
// Simulate a time-consuming task
progressStatus += 1

// Update the progress on the UI thread
handler.post {
simpleProgressBar.progress = progressStatus
}

try {
// Simulate some delay (you can replace this with your actual task)
Thread.sleep(50)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
}.start()
}



}
}

Open Device Manager, run the emulator, and  then run the application. Next, check the working output and check the output  you declared in your code.


Output:




Comments