show Alert Dialog within UI thread using AlertDialog.Builder - Android android.app

Android examples for android.app:AlertDialog

Description

show Alert Dialog within UI thread using AlertDialog.Builder

Demo Code

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class Main {

  private static AlertDialog.Builder aDialogBuilder;
  private static AlertDialog aDialog;

  public static void showAlertDialog(final Activity activity, final String titel, final String text) {

    if (!activity.isFinishing()) {
      activity.runOnUiThread(new Runnable() {

        public void run() {
          aDialogBuilder = new AlertDialog.Builder(activity);
          aDialogBuilder.setMessage(text);
          aDialogBuilder.setTitle(titel);
          aDialogBuilder.setCancelable(false);
          aDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
              dialog.cancel();//w  ww.j a v  a  2  s  .  c  o m

            }
          });
          aDialog = aDialogBuilder.create();
          aDialog.show();
        }
      });
    }

  }

}

Related Tutorials