Custom Dialog Boxes in Android Example

A dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are normally used for notifications that should interrupt the user and to perform short tasks that directly relate to the application in progress . The default dialog example I have already posted before.Here’s how a custom dialog is created.

The Dialog class is the base class for creating dialogs.

Types of default dialog  boxes are:

If we wish to create a customized dialog box then we can do so in the following way:

Step 1. Create a xml layout for the dialog box and a transparent background is preferable in order to show the Activity screen underneath it.


<RelativeLayout xmlns:android=""
    android:background="@drawable/bg_dialog" >

        android:background="@drawable/mid_box" />

        android:background="@drawable/female_selector" />



Step2. Add the following code for dialog box in oncreate(),onclick() etc based on where you want the dialog box to  pop. In the given example the dialog box appears  as soon as the activity starts .Hence, the code is placed in oncreate() .

public void onCreate(Bundle savedInstanceState)
		  setContentView(R.layout.play_menu_screen);//setting layout foractivity

              //put your activity related code here

		Dialog();   //function call for dialog box


	 /*code for creating a custom dialog box  */

	 private void Dialog()
 {    String gender;

// creating a dialog object and specifying the activity it pops on .
// specifies that dialog box has height
// equivalent to screen height

final Dialog dialog = new Dialog(PlayActivity.this,;

	dialog.setContentView(R.layout.dialog_screen);//setting the dialog xml layout

		/* adding action when image buttons of dialog are clicked */
					new OnClickListener() {
						public void onClick(View v) {
						//put your code here
						dialog.dismiss();//closes the dialog box
					});;//pops the dialog box


This way we can create a customized dialog box .Why use the boring default dialog boxes when we can create our own.


Custom Dialog

Custom Dialog