What is the difference between Progressbar and progressDialog ? |
Handling ProgressBars
ProgressBar is a View (like TextView, ImageView, Button, etc.).which can be used in your layout to show some progress. progress Bar is a very common component in all User Interfaces, when you want to display the progress of a task that is taking up a lot of time, for example.
Example:- activity.main.xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_background=”#f123”
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_label" />
</LinearLayout>
MainActivity.xml
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button button;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarbHandler = new Handler();
private long fileSize = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButtonListener();
}
public void addButtonListener() {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// create and display a new ProgressBarDialog
progressBar = new ProgressDialog(view.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
progressBarStatus = 0;
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = downloadFile();
// sleep 1 second (simulating a time consuming task...)
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarbHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// if the file is downloaded,
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// and then close the progressbar dialog
progressBar.dismiss();
}
}
}).start();
}
});
}
// file download simulator...
public int downloadFile() {
while (fileSize <= 1000000) {
fileSize++;
if (fileSize == 100000) {
return 10;
} else if (fileSize == 200000) {
return 20;
} else if (fileSize == 300000) {
return 30;
} else if (fileSize == 400000) {
return 40;
} else if (fileSize == 500000) {
return 50;
} else if (fileSize == 700000) {
return 70;
} else if (fileSize == 800000) {
return 80;
}
//...
}
return 100;
}
}
Android ProgressDialog:-
ProgressDialog is a Dialog with ‘built-in’ ProgressBar. A ProgressDialog is used when we want to prevent the user from interacting with the application while waiting. The Dialog aspect freezes the user from doing anything until it is dismissed.
Android ProgressDialog Attributes
setMax(int
max) – This method sets the maximum value of the progress
dialog.
getMax() – This method return the maximum value of
the progress dialog, basically this method is used while applying condition
over the progress dialog.
getProgess()
– This returns current progress of the progress dialog in
numeric.
incrementProgressBy(int
diff) – This method increments the progress dialog value with
the defined value.
setCancelable(boolean
cancelable) – This method has boolean value i.e true/false. If set to
false it allows to cancel the dialog box by clicking on area outside the dialog
default it is true if method is not used.
dismiss() – This method
dismiss the progressdialog.
Example:activity.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:background="#4D99E6"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:text=" Click To View Progress Dialog..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.DeviceDefault.Button.Inset"
android:layout_marginTop="49dp"
android:textSize="15sp"
android:id="@+id/button2"
android:layout_gravity="center"
android:textStyle="normal|bold"
android:textColor="@android:color/background_dark"
android:layout_centerHorizontal="true" />
</LinearLayout>
Example:MainActivity
package com.example.progressbar;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button b1, b2;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
Handler handle = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressDialog.incrementProgressBy(2); // Incremented By Value 2
}
};
@Override
public void onClick(View v) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMax(100); // Progress Dialog Max Value
progressDialog.setMessage("Loading..."); // Setting Message
progressDialog.setTitle("ProgressDialog"); // Setting Title
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // Progress Dialog Style Horizontal
progressDialog.show(); // Display Progress Dialog
progressDialog.setCancelable(false);
new Thread(new Runnable() {
@Override
public void run() {
try {
while (progressDialog.getProgress() <= progressDialog.getMax()) {
Thread.sleep(200);
handle.sendMessage(handle.obtainMessage());
if (progressDialog.getProgress() == progressDialog.getMax()) {
progressDialog.dismiss();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
0 Comments