Dialog box  in Android :

In Android, a dialog is usually a small floating window that appears in front of the present activity. once a dialog appears, the present activity loses focus and user interaction becomes focused on the dialog.

A dialog is usually wont to get a response from the user. In Android, a dialog is implemented as a dialog fragment, that floats on top of an activity and is displayed Model. Dialog fragments are useful once you need to acquire the user’s response before continuing with the execution or any type of permission etc .

android custom dialog To make a dialog fragment, you will need to extend the DialogFragment base class. This example shows you ways to make 2 styles of dialogs: AN input dialog and a Status dialog.

Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.2 (KitKat Version) and SDK build-tools 22.6.3

Solution Stuff :

For Display dialog box in this example you need  add two Android XML layout files to the res/layout folder and name them as shown in Figure :

android dialog box example

1. Open “res/layout/input_name_dialogfragment.xml” file and put this xml code it into file:

File : res/layout/input_name_dialogfragment.xml

This Userinterface basically contains a page with a label, a text box, and a button which display in dialog.

2. Open “res/layout/yes_no_dialogfragment.xml” file and put this xml code it into file:

File : res/layout/yes_no_dialogfragment.xml

This Userinterface contains a label and two buttons of android dialog box.

Add two new Java classes to the project for Custom Dialog as shown in Figure :

android dialog example

 

3. Open “InputNameDialogFragment.java” file and add following JAVA code.

File : src/package-name/InputNameDialogFragment.java

This class extends the DialogFragment base class and loads the UI defined within the input_name_dialogfragment.xml file. in this class, you created an interface called InputNameDialogListener with an abstract method named onFinishInputDialog(). This method are implemented by the activity that calls this class. In essence, it’s dismissed once the user clicks the button to shut the dialog.The text entered into the text box is then sent back to the calling activity through this method.

4. Open “YesNoDialogFragment.java” file and add following JAVA code.

File : src/package-name/YesNoDialogFragment.java

same as InputNameDialogFragment class, this class extends the DialogFragment class and implements an interface.

5. Open “res/layout/activity_main.xml” file and put this xml code it into file:

File : res/layout/activity_main.xml

6. Open “MainAcitvity.class” file and add following JAVA code.

File : src/package-name/MainActivity.java

Output of  Android Custom Dialog box in emulator :

android custom dilaog

Download Android Custom Dialog box example Source code :

Leave a Reply