Native Mobile App Development With Java

Android ProgressBar

15-Sep-2024

Learn how to use ProgressBar in Android Studio project Java

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 Kotlin




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.java file −

Java:

package com.microappvalley.newtestproject;


import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

ProgressBar simpleProgressBar;
ProgressBar progressBar;
Button buttonStart;

int progressStatus=0;
private Handler handler = new Handler();
Thread thread;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the VideoView in the layout
buttonStart = findViewById(R.id.buttonStart);

simpleProgressBar = findViewById(R.id.simpleProgressBar);
progressBar = findViewById(R.id.progressBar);
simpleProgressBar.setMax(100);
simpleProgressBar.setProgress(10);

progressBar.setVisibility(View.GONE);

buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);


//Progress Bar set Progress
thread = new Thread(new Runnable() {
public void run() {

while (progressStatus < 100) {
progressStatus += 1;

// Update the progress bar on the main thread
handler.post(new Runnable() {
public void run() {
//Set Progress
simpleProgressBar.setProgress(progressStatus);

}
});

try {
// Sleep for a short duration to simulate work being done
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});

thread.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