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.

dialog_screen.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_dialog" >

    <ImageView
        android:id="@+id/dialogImgView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/mid_box" />

    <ImageButton
        android:id="@+id/ImgBtn2_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/ImgBtn1_id"
        android:background="@drawable/female_selector" />

</RelativeLayout>

 

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)
	 {
	        super.onCreate(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 .
// R.style.FullHeightDialog specifies that dialog box has height
// equivalent to screen height

final Dialog dialog = new Dialog(PlayActivity.this,R.style.FullHeightDialog);

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

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

			dialog.show();//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.

Screenshot:

Custom Dialog

Custom Dialog

Advertisements

2 thoughts on “Custom Dialog Boxes in Android Example

  1. Keep up the wonderful piece of work, I read few articles on this site and I conceive that your weblog is rattling interesting and holds bands of wonderful info.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s