Dialog and Alert Dialog in android?

Friends we know that dialog is most important role play on all software development now we are going to learn about the dialog in android development,this tutorial will help to you for creating dialog box in android,please read all my point for learning about Dialogs.

1.What is Dialog in android?

 Dialog is used to display the message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.

2.Type of  Dialog in android?

There are four  type dialog in android.

  1. 1.Dialog
  2. 2.Alert Dialog
  3. 3.Toast

3.Simple Dialog

A simple dialog is small window for prompts the user to a decision or enter additional information.if you want to create simple dialog please fread out blow code

Example :- You can use this code any button click and as your requirement?

   // Create custom dialog object
     final Dialog dialog = new Dialog(CustomDialog.this);

  // Include dialog.xml file
     dialog.setContentView(R.layout.dialog);

  // Set dialog title
     dialog.setTitle("Custom Dialog");
 
   // set values for custom dialog components - text, image and button
      TextView text = (TextView) dialog.findViewById(R.id.textDialog);
                text.setText("Custom dialog Android example.");

     ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
 image.setImageResource(R.drawable.yourimage);

  //show popup window by calling show meth0d
     dialog.show();
                 
     Button declineButton = (Button)    dialog.findViewById(R.id.declineButton);
   // if decline button is clicked, close the custom dialog
        declineButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Close dialog
                        dialog.dismiss();
                    }
                });
Here we are creating xml file which we want show for popup window.

dialog.xml    File

  

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
  
    <ImageView
        android:id="@+id/imageDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dp" />

    <TextView
        android:id="@+id/textDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        android:layout_toRightOf="@+id/imageDialog"/>
  
     <Button
        android:id="@+id/declineButton"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Submit "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/textDialog"
        android:layout_toRightOf="@+id/imageDialog"
        />
      
</RelativeLayout>    

4.Alert Dialog

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.
Android AlertDialog is composed of three regions: title, content area and action buttons.

Note -  The Android AlertDialog in android  is the subclass of Dialog class.

If you want to create Alert Dialog you need to make an object of AlertDialogBuilder which an inner class of AlertDialog. Its syntax is given below

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
The Complete Example of Alert Dialog see in below-

4.1 Create Alert dialog 

 AlertDialog.Builder builder = new AlertDialog.Builder(this);  
 //Uncomment the below code to Set the message and title from the strings.xml file  
builder.setMessage(R.string.dialog_message)                              builder.setTitle(R.string.dialog_title);  
          
//Setting message manually and performing action on button click  
 builder.setMessage("Do you want to close this application ?")  
            .setCancelable(false)  
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int id) {  
                finish();  
                }  
            })  
            .setNegativeButton("No", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int id) {  
                //  Action for 'NO' Button  
                dialog.cancel();  
             }  
            });  
  
        //Creating dialog box  
        AlertDialog alert = builder.create();  
        //Setting the title manually  
        alert.setTitle("AlertDialogExample");  
        alert.show();  
        
    }  
               

5. Toast 

Toast display a massage for the short period of time. A Toast contains message to be displayed quickly and disappears after sometime.
You can also create custom toast as well for example toast displaying image. You can visit next page to see the code for custom toast.

//display in short period of time
Toast.makeText(getApplicationContext(), "your  msg", Toast.LENGTH_SHORT).show();

//display in long period of time
Toast.makeText(getApplicationContext(), "your msg", Toast.LENGTH_LONG).show();


Note  -if you have any issue regarding this post please comment me on comments box?

Thanks?