display Message Dialog - Android User Interface

Android examples for User Interface:Alert Dialog

Description

display Message Dialog

Demo Code


//package com.java2s;

import android.app.AlertDialog;

import android.content.Context;
import android.content.DialogInterface;

public class Main {
    /**//from w  w  w  .  j a va 2s .c  o m
     * @fn public void displayMessageDialog(String message, String title)
     * @brief Displays a message dialog to the user.
     * Displaying message dialogs from http://www.mkyong.com/android/android-alert-dialog-example/
     * @param message Message to display
     * @param title Title of dialog
     * 
     */

    public static void displayMessageDialog(Context context, String title,
            String message) {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        // set title
        if (title == null) {
            title = "Message";
        }
        alertDialogBuilder.setTitle(title);

        // set dialog message
        alertDialogBuilder
                .setMessage(message)
                .setCancelable(false)
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }
}

Related Tutorials