ProgressDialog/AlertDialog/CustomAlertDialog
1
Sourabh Sahu
ProgressDialog progressDialog;
• ProgressDialog progressDialog;
• //In OnCreate Method
• progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading..."); // Setting
• Message progressDialog.setTitle("ProgressDialog"); // Setting
Title
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNE
R); // Progress Dialog Style Spinner progressDialog.show(); //
• Display Progress Dialog progressDialog.setCancelable(false);
new Thread(new Runnable() {
• public void run() {
• try {
• Thread.sleep(10000);
• }
• catch (Exception e) {
• e.printStackTrace();
• }
• progressDialog.dismiss();
• }
• }).start();
Horizontal Progress Dialog
• 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_HORIZON
TAL); // 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();
Methods
1. setTitle(CharSequence title) – This component is used to set the
title of the progress dialog.
// Setting Title progressDialog.setTitle("ProgressDialog");
2. setMessage(CharSequence message) – This component displays
the required message in the progress dialog.
// Setting Message progressDialog.setMessage("Loading...");
3. setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) – This is
used for setting the horizontal style of the progress dialog.
// Progress Dialog Style Horizontal
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
4. setProgressStyle(ProgressDialog.STYLE_SPINNER) – This is used
for setting the spinner style of the progress dialog.
// Progress Dialog Style Spinner
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
•
5. setMax(int max) – This method sets the maximum value of the progress dialog.
• // Progress Dialog Max Value progressDialog.setMax(100);
• 6. getMax() – This method return the maximum value of the progress dialog,
basically this method is used while applying condition over the progress dialog.
• // Fetching max value progressDialog.getMax();
• 7. getProgess() – This returns current progress of the progress dialog in numeric.
• // Fetching current progress progressDialog.getProgress();
• 8. incrementProgressBy(int diff) – This method increments the progress dialog
value with the defined value.
• // Incremented By Value 2 progressDialog.incrementProgressBy(2);
• 9. 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.
• // Cannot Cancel Progress Dialog progressDialog.setCancelable(false);
• 10. dismiss() – This method dismiss the progressdialog.
• //Dismiss the dialog progressDialog.dismiss();
AlertDialog
• AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
• // Setting Alert Dialog Title
• alertDialogBuilder.setTitle("Confirm..!!!");
• // Icon Of Alert Dialog alertDialogBuilder.setIcon(R.drawable.ask);
• // Setting Alert Dialog Message
• alertDialogBuilder.setMessage(“Do you want to continue");
alertDialogBuilder.setCancelable(false);
• alertDialogBuilder.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
• @Override public void onClick(DialogInterface arg0, int arg1) {
• finish(); } });
• alertDialogBuilder.setNegativeButton("No", new
DialogInterface.OnClickListener() { @Override public void
onClick(DialogInterface dialog, int which)
{ Toast.makeText(MainActivity.this,"You
Continued",Toast.LENGTH_SHORT).show(); } });
• alertDialogBuilder.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
• @Override public void onClick(DialogInterface
dialog, int which)
{ Toast.makeText(getApplicationContext(),“You
have canceled the
choice",Toast.LENGTH_SHORT).show(); } });
• AlertDialog alertDialog =
alertDialogBuilder.create(); alertDialog.show();
• 1. setTitle(CharSequence title) – This component is used to
set the title of the alert dialog. It is optional component.
• // Setting Alert Dialog Title
alertDialogBuilder.setTitle(“Confirm..!!!");
• 2. setIcon(Drawable icon) – This component add icon before
the title. You will need to save image in drawable icon.
• // Icon Of Alert Dialog alertDialogBuilder.setIcon(R.drawable.ask);
• 3. setMessage(CharSequence message) – This component
displays the required message in the alert dialog.
• // Setting Alert Dialog Message
alertDialogBuilder.setMessage("Are you sure,Dou you want to
continue");
• 4. setCancelable(boolean cancelable) – This component 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 else it allows.
• alertDialogBuilder.setCancelable(false);
• 5. setPositiveButton(CharSequence text, DialogInterface.OnClickListener
listener) – This component add positive button and further with this user confirm he
wants the alert dialog question to happen.
• alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener()
{ @Override public void onClick(DialogInterface arg0, int arg1) { finish(); } });
• 6. setNegativeButton(CharSequence text, DialogInterface.OnClickListener
listener) – This component add negative button and further with this user confirm
he doesn’t want the alert dialog question to happen.
• alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener()
{ @Override public void onClick(DialogInterface dialog, int which)
{ Toast.makeText(MainActivity.this,"You clicked over
No",Toast.LENGTH_SHORT).show(); } });
• 7. setNeutralButton(CharSequence text, DialogInterface.OnClickListener
listener) – This component simply add a new button and on this button developer
can set any other onclick functionality like cancel button on alert dialog.
• alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener()
{ @Override public void onClick(DialogInterface dialog, int which)
{ Toast.makeText(getApplicationContext(),"You clicked on
Cancel",Toast.LENGTH_SHORT).show(); } });
Custom Alert
• To Make Custom Alert,You have design a
layout that will be inflatedto
AlertDialogBuilder
Customalert.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/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <
• TextView android:text="@string/alert"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/text"
android:textStyle="normal|bold" android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/image"
android:layout_toEndOf="@+id/image" android:layout_marginLeft="21dp"
android:layout_marginStart="21dp" android:layout_marginTop="12dp" />
<
• ImageButton android:layout_width="35dp"
android:layout_height="35dp" app:srcCompat="@drawable/stop"
android:id="@+id/image" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
tools:ignore="ContentDescription,RtlHardcoded" /> <
• Button android:id="@+id/dialogButtonOK"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/dismiss"
android:layout_marginTop="23dp" android:paddingRight="5dp"
android:layout_below="@+id/image"
android:layout_alignLeft="@+id/image"
android:layout_alignStart="@+id/image" android:textSize="18sp"
style="@style/Widget.AppCompat.Button.Colored“ />
</RelativeLayout>
Setting View
• final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom); Button
dialogButton = (Button)
dialog.findViewById(R.id.dialogButtonOK); // if
button is clicked, close the custom dialog
dialogButton.setOnClickListener(new
View.OnClickListener() { @Override public void
onClick(View v) { dialog.dismiss();
Toast.makeText(getApplicationContext(),"Dismiss
ed..!!",Toast.LENGTH_SHORT).show(); } });
dialog.show();
Thank You
16

Progress Dialog, AlertDialog, CustomDialog

  • 1.
  • 2.
    ProgressDialog progressDialog; • ProgressDialogprogressDialog; • //In OnCreate Method • progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setMessage("Loading..."); // Setting • Message progressDialog.setTitle("ProgressDialog"); // Setting Title progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNE R); // Progress Dialog Style Spinner progressDialog.show(); // • Display Progress Dialog progressDialog.setCancelable(false); new Thread(new Runnable() { • public void run() { • try {
  • 3.
    • Thread.sleep(10000); • } •catch (Exception e) { • e.printStackTrace(); • } • progressDialog.dismiss(); • } • }).start();
  • 4.
    Horizontal Progress Dialog •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_HORIZON TAL); // 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()) {
  • 5.
    • Thread.sleep(200); • handle.sendMessage(handle.obtainMessage()); •if (progressDialog.getProgress() == progressDialog.getMax()) { • progressDialog.dismiss(); • } • } • } catch (Exception e) { • e.printStackTrace(); • } } }).start();
  • 6.
    Methods 1. setTitle(CharSequence title)– This component is used to set the title of the progress dialog. // Setting Title progressDialog.setTitle("ProgressDialog"); 2. setMessage(CharSequence message) – This component displays the required message in the progress dialog. // Setting Message progressDialog.setMessage("Loading..."); 3. setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) – This is used for setting the horizontal style of the progress dialog. // Progress Dialog Style Horizontal progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 4. setProgressStyle(ProgressDialog.STYLE_SPINNER) – This is used for setting the spinner style of the progress dialog. // Progress Dialog Style Spinner progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  • 7.
    • 5. setMax(int max)– This method sets the maximum value of the progress dialog. • // Progress Dialog Max Value progressDialog.setMax(100); • 6. getMax() – This method return the maximum value of the progress dialog, basically this method is used while applying condition over the progress dialog. • // Fetching max value progressDialog.getMax(); • 7. getProgess() – This returns current progress of the progress dialog in numeric. • // Fetching current progress progressDialog.getProgress(); • 8. incrementProgressBy(int diff) – This method increments the progress dialog value with the defined value. • // Incremented By Value 2 progressDialog.incrementProgressBy(2); • 9. 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. • // Cannot Cancel Progress Dialog progressDialog.setCancelable(false); • 10. dismiss() – This method dismiss the progressdialog. • //Dismiss the dialog progressDialog.dismiss();
  • 8.
    AlertDialog • AlertDialog.Builder alertDialogBuilder= new AlertDialog.Builder(this); • // Setting Alert Dialog Title • alertDialogBuilder.setTitle("Confirm..!!!"); • // Icon Of Alert Dialog alertDialogBuilder.setIcon(R.drawable.ask); • // Setting Alert Dialog Message • alertDialogBuilder.setMessage(“Do you want to continue"); alertDialogBuilder.setCancelable(false); • alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { • @Override public void onClick(DialogInterface arg0, int arg1) { • finish(); } }); • alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"You Continued",Toast.LENGTH_SHORT).show(); } });
  • 9.
    • alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener(){ • @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),“You have canceled the choice",Toast.LENGTH_SHORT).show(); } }); • AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show();
  • 10.
    • 1. setTitle(CharSequencetitle) – This component is used to set the title of the alert dialog. It is optional component. • // Setting Alert Dialog Title alertDialogBuilder.setTitle(“Confirm..!!!"); • 2. setIcon(Drawable icon) – This component add icon before the title. You will need to save image in drawable icon. • // Icon Of Alert Dialog alertDialogBuilder.setIcon(R.drawable.ask); • 3. setMessage(CharSequence message) – This component displays the required message in the alert dialog. • // Setting Alert Dialog Message alertDialogBuilder.setMessage("Are you sure,Dou you want to continue"); • 4. setCancelable(boolean cancelable) – This component 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 else it allows. • alertDialogBuilder.setCancelable(false);
  • 11.
    • 5. setPositiveButton(CharSequencetext, DialogInterface.OnClickListener listener) – This component add positive button and further with this user confirm he wants the alert dialog question to happen. • alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { finish(); } }); • 6. setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) – This component add negative button and further with this user confirm he doesn’t want the alert dialog question to happen. • alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"You clicked over No",Toast.LENGTH_SHORT).show(); } }); • 7. setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener) – This component simply add a new button and on this button developer can set any other onclick functionality like cancel button on alert dialog. • alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"You clicked on Cancel",Toast.LENGTH_SHORT).show(); } });
  • 12.
    Custom Alert • ToMake Custom Alert,You have design a layout that will be inflatedto AlertDialogBuilder
  • 13.
    Customalert.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/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> < • TextView android:text="@string/alert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" android:textStyle="normal|bold" android:textSize="18sp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/image" android:layout_toEndOf="@+id/image" android:layout_marginLeft="21dp" android:layout_marginStart="21dp" android:layout_marginTop="12dp" /> <
  • 14.
    • ImageButton android:layout_width="35dp" android:layout_height="35dp"app:srcCompat="@drawable/stop" android:id="@+id/image" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" tools:ignore="ContentDescription,RtlHardcoded" /> < • Button android:id="@+id/dialogButtonOK" android:layout_width="200dp" android:layout_height="wrap_content" android:text="@string/dismiss" android:layout_marginTop="23dp" android:paddingRight="5dp" android:layout_below="@+id/image" android:layout_alignLeft="@+id/image" android:layout_alignStart="@+id/image" android:textSize="18sp" style="@style/Widget.AppCompat.Button.Colored“ /> </RelativeLayout>
  • 15.
    Setting View • finalDialog dialog = new Dialog(context); dialog.setContentView(R.layout.custom); Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); // if button is clicked, close the custom dialog dialogButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); Toast.makeText(getApplicationContext(),"Dismiss ed..!!",Toast.LENGTH_SHORT).show(); } }); dialog.show();
  • 16.