Dialog`s for Android: Chapter two – Simple Dialog
Here we have an example of Dialog in Android, much similar to previous example for Alert Dialog explained in my previous post, but more customizable. In this tutorial we are going to create one very simple dialog box with only title, and message for showing the user, and one dialog more complex, with own layout, title and buttons for managing what we need.


NOTE: In this example, for binding the Views from XML resources we use ButterKnife library by Jake Wharton.

just put this code in build.gradle file in your app folder and sync it.

NOTE: This example is build like any other android application, which mean that every thing goes in a right place, like no hard-coded strings, they are put in string.xml resources, color.xml, layout resources and getting them from there.

This example project have only one HomeActivity.java, activity_home.xml for the activity layout, dialog_simple.xml layout for the first dialog, and dialog_custom.xml layout for the second dialog.

Lets describe how thing work out with the Simple Dialog:

First the annotations.
They are from the ButterKnife Library as mentioned before, and can be quite helpful.

The example above is functional button, declared as a button, extracted or linked with the correct layout and given its functionality. If you like to learn more about using the ButterKnife Library, i strongly suggest visiting its site:
http://jakewharton.github.io/butterknife/

Now, simpleDialog()

In this example we have method for calling and creating a simple dialog box for showing a message to the user. We create a new Dialog object which need a context as parameter, and in our case this is the correct form because we get our context from the Home Activity. If you are working in Views or Rich Views as some are calling them, you will need to pass getContext() method as parameter. Next we need to tell our dialog object where to look for our layout with the setContentView() method which takes layout resource ID in int type. We pass our resource layout with R.layout.some_layout_we_have_created in .xml format, in layout folder. At this point we have fully developed dialog, but, it is not shown on the screen without .show() method. With this final command, we have created and shown the dialog to the user

dialog_simple (2)

And here is the layout example code for it, located in dialog_simple.xml in layout resource folder in our android application project.

Now lets explain how our Custom Dialog (which is derivative of our Simple Dialog) works:

As mentioned before, we have a button for showing our custom dialog, witch calls our customDialog() method.

We must, again, create new Dialog object with same parameters as before, but with a slight difference with the first keyword final which we must declare our object as final so it can be accessed in our inner, on click listener, classes for the buttons, as shown in our example. Setting the content View with our custom layout is declared in our dialog_custom.xml layout resource file. Then TextView-s are declared for the buttons, OK and cancel, message for the user, and the dialog title. All of these fields are declared, alsow, in the dialog_custom.xml layout. After the declaration and initialization by dialog.findViewById(R.id.what_ever_we_like_to_do), because all of the views are TextView-s, we setText() to them. Because our method customDialog() have two parameter, we must give it some strings for the title and message in it, which is done in our example by calling some string resources, but you can get the parameters from wherever you like, but i strongly recommend not to hard code them.
At the end, we set onClickListeners for our previously declared buttons so the user can interact with the dialog. They both dismiss the dialog box in this case, but it can do whatever you like, like storing some strings in Shared Preferences, or calling some web service after some button is clicked, and so on, an so on.
Here is the layout code for the Custom Dialog explained before:

dialog_custom (2)

and the layout for the HomeActivity:

Here is the link for the whole example project:
https://www.dropbox.com/s/i812874m4bvnuey/SimpleDialogExample.rar?dl=0

Spread the love

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *