• Have Any Question?
  • contact@eitworld.com

Dialogs in Android





  • A dialog is like a small window which can appear in the front of the activity.
  • We can take input by using the dialogs in android.
  • Dialogs are used to show the notification which can interrupt the user and can be used to perform the short tasks which is directly related to the current activity.
  • There are several kinds of the dialogs in android. These are as follows:
    • Alert Dialog: it is a simple alert dialog box. We can define the title for this and we can also add three buttons and can display the list of items that can be selected by the user. To create it, we have to extend the AlertDialog class object.
    • DatePicker Dialog: this type of dialog shows a window and user can select the date from this window. To create the date picker dialog, we have to extend the DatePicker class.
    • Time picker dialog: this dialog is used to select the time. To create this dialog, we have to extend the TimePicker class.
  • All the dialogs classes extend the Dialog class.
  • If we want to use the dialogs in the application then first we have to create the container for the dialog. To do this, we have to DialogFragment class and this class provides the several methods to create the dialogs.

Creating Android Dialog

  • We can create the dialogs using the following statements:
    AlertDialog.Builder yrobj = new AlertDialog.Builder(getActivity());
    //creating alert dialog
    yrobj.setMessage(R.string.yrmessage); //setting message
    yrobj.setTitle(R.string.yrtitle); // setting title
    AlertDialog ad = yrobj.create();
    // getting alert dialog from AlertDialog.Builder
  • To create the dialog, first we have to create the container for the dialog fragment. To do this, we have to extend the DialogFragment class and inside this class, we have to implement the onCreateDialog() method.
  • Inside onCreateDialog() method, we have to create the dialog. To create the dialog, we use the Builder class.
  • The basic syntax is as follows:
    public class class_name extends DialogFragment
    {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
    // create dialog here
    }
    }
  • When we call the show() method by using the object of this class then the dialog will show.


Real Time Web Analytics