0% found this document useful (0 votes)
15 views2 pages

Practical No 13

The document contains an XML layout for a simple Android application featuring a horizontal ProgressBar and a button to start the progress. The Java code in MainActivity manages the progress bar's status, incrementing it in a background thread while updating the UI on the main thread. The application allows users to visualize progress through the ProgressBar as they interact with the button.

Uploaded by

Vaman Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Practical No 13

The document contains an XML layout for a simple Android application featuring a horizontal ProgressBar and a button to start the progress. The Java code in MainActivity manages the progress bar's status, incrementing it in a background thread while updating the UI on the main thread. The application allows users to visualize progress through the ProgressBar as they interact with the button.

Uploaded by

Vaman Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical No 13

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- ProgressBar widget -->
<ProgressBar
android:id="@+id/progressBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<!-- Button to start progress -->
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Progress"
android:layout_below="@id/progressBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
</RelativeLayout>

Main.java

package com.example.progress_bar;

import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private Button startButton;
private Handler handler = new Handler();
private int progressStatus = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
startButton = findViewById(R.id.startButton);
startButton.setOnClickListener(v -> startProgress());
}

private void startProgress() {


// Reset progress to 0 before starting
progressStatus = 0;
progressBar.setProgress(progressStatus);
// Create a new thread to update progress in the background
new Thread(new Runnable() {
@Override
public void run() {
while (progressStatus < 100) {
progressStatus++; // Increment progress
try {
Thread.sleep(100); // Sleep for a short time to simulate work
} catch (InterruptedException e) {
e.printStackTrace();
}

// Update the progress on the main thread


handler.post(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progressStatus);} });}}}). start() ;}}

OUTPUT:

You might also like