0% found this document useful (0 votes)
19 views3 pages

Prac 13

The document provides code examples for implementing two types of progress bars in an Android application. The first example demonstrates a circular indeterminate progress bar, while the second example shows a horizontal progress bar within a dialog that updates as a file downloads. Both implementations are presented with XML layout and Java code for the main activity.

Uploaded by

ankitasurya663
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)
19 views3 pages

Prac 13

The document provides code examples for implementing two types of progress bars in an Android application. The first example demonstrates a circular indeterminate progress bar, while the second example shows a horizontal progress bar within a dialog that updates as a file downloads. Both implementations are presented with XML layout and Java code for the main activity.

Uploaded by

ankitasurya663
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/ 3

prac13 - Progress Bar Programs

1. Circular Progress Bar (Indeterminate)

XML (activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<ProgressBar
android:id="@+id/progress_circular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="visible"/>
</LinearLayout>

Java (MainActivity.java):
package com.example.progressbarapp;

import android.os.Bundle;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ProgressBar progressBar = findViewById(R.id.progress_circular);


progressBar.setIndeterminate(true);
}
}

2. Horizontal Progress Bar with Dialog

XML (activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/buttonDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DOWNLOAD FILE"/>
</LinearLayout>

Java (MainActivity.java):
package com.example.progressbarapp;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


private ProgressDialog progressDialog;
private int progressStatus = 0;
private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button buttonDownload = findViewById(R.id.buttonDownload);


buttonDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("File downloading ...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.setMax(100);
progressDialog.show();

new Thread(new Runnable() {


public void run() {
while (progressStatus < 100) {
progressStatus += 10;
handler.post(new Runnable() {
public void run() {
progressDialog.setProgress(progressStatus);
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start();
}
});
}
}

You might also like