Android Dialogs : What, When and How (App Development)

Photo by Eduardo Rosas from Pexels

Dialogs in Android App, are something, that we can’t avoid, be it Confirmation dialog, Alert dialog or even calendar. Doing it the wrong way, can affect the user experience drastically.

While using it frequently in multiple activities, making it re usable, is the best practice. So we should develop it through fragment, which is reusable, can interact with parent activity and not the least, will be good for code maintenance.

There can be different kind of dialogs, according to the need, like:-

  1. Alert Dialogs :- Used to just show information, and is not interactive, in general.
  2. Confirmation Dialogs :- Used to take consent from user, if one is comfortable with the proceeding action, or even want to cancel that.
  3. Calendar Dialogs :- To take date input from user.
  4. Dynamic Dialog with fields or list, to take user input.

In this article, we will cover confirmation and calendar dialogs.

Confirmation Dialogs

Normally this kind of dialog contain a simple title, a message, and actions that user can take, either positive, negative or neutral. Below, is a simple dialog, to confirm user exit.

Lets check, how to make one, like above.

We are using Android studio, for the code. First we need to add a simple fragment like below. A blank fragment will do the the work for us, and remove all the dummy code that android studio adds by default.

In order to create a dialog through a fragment, we need to extend DialogFragment class.

First method we need to have is the parent class method onCreateDialog, which we will override here, to customize the title, message and actions.

Above code does nothing, but simply sets the title and message for alert dialog. To set Actions, we need to define action methods like below

here, we have added the action texts, i.e. Yes or No, and also, what we intent to do on clicking it through OnClickListener function. And that is all, for a simple static dialog.

Now, in order to call it from our activity ui, we need to have some static method that can be called to invoke it. We need to add a method, similar to below:-

We can also use above method to get some data from UI as input for the dialog. For e.g. title, or message, which we can further pass to our previous method onCreateDialog, to make them dynamic. So, the code will need some change like below:-

We have used simple bundle, to pass data from static method to onCreateDialog.

As, our dialog is ready to be used by activity, we can use code, something like below to invoke it from the parent activity.

But now, we need to have some criteria, in order to listen back the actions that we perform on dialog, and send them back to activity. For that purpose, we can follow interface approach.

What we need to do is, create a public interface in our fragment class, and declare a method, which will work as listener, like below:-

And get the context of activity, through onAttach method.

And call the listener method, on Positive or Negative action.

And then, in our activity, first we need to implement the interface and then define its function definition like below

public class DemoActivity extends AppCompatActivity        implements DemoFragment.ConfirmationDialogListener {

So, that is all about the Confirmation Dialog. What we have just done is:-

  1. Create a confirmation dialog as fragment.
  2. Invoke the same from activity.
  3. Listen the actions in activity.

Calendar Dialog

Calendar dialog, is just similar to above, but we need to override and listen to onDateSet method for getting and sending the date to activity, instead of Positive or Negative actions, because we don’t have those actions here.

We need to call the interface listener function, on the onDateSet method.

And the onCreateDialog will look like below:-

To call it from parent activity, we need to create similar getInstance() static function, with or without parameters, as created previously, and call it from parent activity

And, implementing the interface in our parent activity, will serve the purpose here, to get the selected date.

There is much more about dialogs, like to create a user input form, and getting the data, through dialog. That we can reserve for the next story.

Stay tuned.

Blogging | Reading | Playing Chess | Android App Developer | Continuous Learner.