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

PR-13-ProgressBar With ProgressDialog Class

This document describes how to implement a progress bar using the ProgressDialog class in Android. It includes code for a MainActivity class that initializes a ProgressDialog on button click, sets its properties like style and maximum value, displays it, and uses a TimerTask to increment the progress value over time until it reaches 100, at which point the dialog is dismissed. It also includes the layout file for the activity's user interface with a text view, button, and progress bar.

Uploaded by

navteshdeore19
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)
130 views2 pages

PR-13-ProgressBar With ProgressDialog Class

This document describes how to implement a progress bar using the ProgressDialog class in Android. It includes code for a MainActivity class that initializes a ProgressDialog on button click, sets its properties like style and maximum value, displays it, and uses a TimerTask to increment the progress value over time until it reaches 100, at which point the dialog is dismissed. It also includes the layout file for the activity's user interface with a text view, button, and progress bar.

Uploaded by

navteshdeore19
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- Develop a Program to implement ProgressBar with ProgressDialog Class

MainActivity. Java

package com.example.progressdialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {
Button btn;
ProgressDialog pd;
int counter=0,max;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.button);
btn.setOnClickListener(this);

}
public void onClick(View v) {
pd=new ProgressDialog(MainActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(100);
pd.setIndeterminate(false);
pd.setTitle("Android Developers");
pd.setMessage("Downloading File");
pd.show();
Timer t=new Timer();
TimerTask tsk= new TimerTask() {
public void run() {
counter++;
pd.setProgress(counter);

if(counter==100)
{
t.cancel();
}
}
};
t.schedule(tsk,0,100);

} }
activity_main.xml

<?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"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="182dp"
android:layout_height="54dp"
android:gravity="center"
android:text="ProgressDialog"
android:textColor="@color/purple_500"
android:textSize="24sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="159dp"
android:layout_marginLeft="159dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="159dp"
android:layout_marginRight="159dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

You might also like