0% found this document useful (0 votes)
34 views7 pages

Exp 13

The document contains XML and Java code for an Android app that displays a progress dialog when downloading a file. The XML code defines a button to trigger the download. The Java code implements an onClick listener for the button that shows a progress dialog on the main thread and runs a background thread to increment the progress value over time, dismissing the dialog once complete.

Uploaded by

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

Exp 13

The document contains XML and Java code for an Android app that displays a progress dialog when downloading a file. The XML code defines a button to trigger the download. The Java code implements an onClick listener for the button that shows a progress dialog on the main thread and runs a background thread to increment the progress value over time, dismissing the dialog once complete.

Uploaded by

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

EXP 13

2
XML CODE
<?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">

<Button
android:id="@+id/btnDownloadFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download File"
android:layout_centerInParent="true"/>

</RelativeLayout>

JAVA CODE
package com.example.progrogress;

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

public class MainActivity extends AppCompatActivity {


Button btnDownloadFile;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDownloadFile =
findViewById(R.id.btnDownloadFile);
btnDownloadFile.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = new ProgressDialog
(MainActivity.this);

progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZ
ONTAL);
progressDialog.setTitle("File
Downloading");
progressDialog.setProgress(100);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
int progress = 0;
while (progress <=100){
try {

progressDialog.setProgress(progress);
progress++;
Thread.sleep(200);
}catch (Exception ex){

}
}
progressDialog.dismiss();
}
});
t.start();
progressDialog.show();
}
});
}
}
OUTPUT

EXTRA

XML
<?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/t
ools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ProgressBar
android:id="@+id/pb2"

android:progress="00"
android:layout_width="200dp"
android:layout_height="150dp"
style="?
android:progressBarStyleHorizontal"

android:layout_centerInParent="true"
/>

<Button
android:id="@+id/btn1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:layout_below="@+id/pb2"
android:layout_centerHorizontal="true"
android:text="Start"
android:layout_marginTop="50dp"/>

</RelativeLayout>

package com.example.exp13;

import
androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends


AppCompatActivity {
ProgressBar pb1,pb2;
Button btn1;
int progress =0;
@Override
protected void onCreate(Bundle
savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// pb1 = findViewById(R.id.pb1);
pb2 = findViewById(R.id.pb2);
btn1 = findViewById(R.id.btn1);

btn1.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {

Thread thread = new


Thread(new Runnable() {
@Override
public void run() {
try {

while(progress !=100) {
progress
= progress + 5;
//
pb1.setProgress(progress);

pb2.setProgress(progress);

Thread.sleep(200);
}
}
catch
(InterruptedException e){
e.printStackTrace();
}
}
});
thread.start();
}
});
}
}

You might also like