About Prahlad Yeri

Prahlad is a freelance software developer working on web and mobile application development. He also likes to blog about programming and contribute to opensource.

How to create android dialogs in a reusable manner

droid-man Creating dialogs is a very common need in your app to show a dialog box to the user in order to fetch a value, be in a mobile, desktop or even a web application. Furthermore, the values can range from anything like simple OK-Cancel dialog results to a list of “check-able” values or even a date-range. I experienced the need to create an android dialog for each one of those for showing reports in a recent android app project. Whilst the java api offers maximum flexibility in creating dialog interface elements, there is no ready-made method that can be called to get, say a result for a message-dialog like this:

 

result = MessageBox.Show();

Other languages like C# and VB provide such methods to show modal dialog boxes that return values after waiting for a modal dialog. But unfortunately, there is no concept of “modal” in android. A thread cannot just sit idle waiting for input as the resources are too valuable for that. Instead, there is the concept of callbacks, so that instead of you waiting for the dialog to return, the method calls back a function reference you have passed it:

AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("Milk supply tracker");
builder.setMessage(message);
builder.setPositiveButton("Yes",listener);
builder.setNegativeButton("No",listener);
builder.create().show();

The listener here is the referece to a function that will be called when the Yes or No button will be clicked. This not only complicates your code, but makes it very difficult to reuse code for handling different situations like getting a selection from a range of values or getting a date/time range. To solve this problem, I created a separate java class called Dialog and added variations of ShowDialog() methods to handle each type of dialog:

class Dialog
{
    public static void ShowMessageDialog(Context context, String message)
    {
        ShowDialog(context,message,MessageBoxType.OKOnly,new String[]{},false, null,null);
    }
 
    public static void ShowMessageDialog(Context context, String message, MessageBoxType type , OnClickListener listener)
     {
         ShowDialog(context,message,type,new String[]{},false, listener,null);
     }
 
    public static void ShowListDialog(Context context, String message, String[] listItems, boolean isMultiChoice, OnClickListener listener)
     {
     if (isMultiChoice)
         ShowDialog(context, message, MessageBoxType.OkCancel , listItems, isMultiChoice, listener,null);
     else
         ShowDialog(context, message, MessageBoxType.OKOnly , listItems, isMultiChoice, null,listener);
     }
 
    public static void ShowDateDialog(Context context,String message,OnDateSetListener listener)
    {
        Calendar c=Calendar.getInstance();
        int y=c.get(Calendar.YEAR);
        int m=c.get(Calendar.MONTH);
        int d=c.get(Calendar.DAY_OF_MONTH);
 
        DatePickerDialog dlg=new DatePickerDialog(context, listener, y, m, d);
        dlg.setTitle(message);
        dlg.show();
    }
 
}

As you can see, the ShowMessageDialog() accepts different parameters depending on whether a listener is required or not. ShowListDialog(), on the other hand passes an array of strings to create a dialog displaying a list of values from which a user may select. The isMultiChoice parameter tells whether a checkbox is required or not against each value in the select list. All this is actually implemented in the ShowDialog() private method, whereas the ShowDateDialog() has its own implementation. Here is the source for ShowDialog():

private static void ShowDialog(Context context, String message, MessageBoxType type , String[] listItems, boolean isMultiChoice, OnClickListener listener,OnClickListener selectedItemListener)
{
    AlertDialog.Builder builder=new AlertDialog.Builder(context);
 
    if (listItems.length>0 && isMultiChoice==false)
    {
        CheckedItems=new ArrayList();//won't be used in this case.
        builder.setTitle(message);
 
        builder.setItems(listItems, selectedItemListener);
    }
    else if (listItems.length>0 && isMultiChoice==true)
    {
        CheckedItems=new ArrayList();
        builder.setTitle(message);
 
        builder.setMultiChoiceItems(listItems, null, new OnMultiChoiceClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean checked) 
            {
                if (checked)
                    CheckedItems.add(which);
                else
                {
                    if (CheckedItems.contains(which))
                        CheckedItems.remove(which);
                }
            }
        });
    }
    else
    {
        builder.setTitle("Milk supply tracker");
        builder.setMessage(message);
    }
 
    if (listItems.length==0 || isMultiChoice)
    {
        switch(type)
        {
        case OKOnly:
            builder.setPositiveButton("OK",listener);
            break;
        case OkCancel:
            builder.setPositiveButton("OK",listener);
            builder.setNegativeButton("Cancel",listener);
            break;
        case YesNo:
            builder.setPositiveButton("Yes",listener);
            builder.setNegativeButton("No",listener);
            break;
        }			
    }
 
    builder.create().show();
}

So hopefully, this class should suffice all your needs related to showing a dialog on your android app. Here is a working example of how the ShowListDialog() is actually called with checkboxes on:

selItems=new String[]{"apples","oranges","grapes"};
Device.ShowListDialog(this,"Select a fruit" ,this.selItems, true, new DialogInterface.OnClickListener() {
 
        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
            if (which==DialogInterface.BUTTON_POSITIVE)
            {
                for(int i:Device.CheckedItems)
                Dialog.ShowMessageDialog(ReportsActivity.this, "selected:" + selItems[i]);
            }
        }
    });
}
Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

Leave a Reply


× 7 = fifty six



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books