show Alert and add click listener for OK button - Android User Interface

Android examples for User Interface:Alert Dialog

Description

show Alert and add click listener for OK button

Demo Code


//package com.java2s;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class Main {
    public static void showAlert(Activity act, String title, String msg) {
        AlertDialog dialog = new AlertDialog.Builder(act).create();
        dialog.setCancelable(true);//  w  ww. j a  va  2 s  .  co m

        if (title != null)
            dialog.setTitle(title);
        if (msg != null)
            dialog.setMessage(msg);

        dialog.setButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}

Related Tutorials