What is the best way to have reusable dialogs?

What is the best way to create reusable dialogs in Android?

Reading through the Dev Dialog Guide , I know what I can use AlertDialog.Builder(this);in one of my Activitys, but what if I want to use this in several actions? If it were some other class, I would expand it, therefore MyDialog extends AlertDialog, but then I can not use Builder.

Any suggestions?

+3
source share
1 answer

Contain one class file like this AllMethod.javaand add this code to this class file.

public static void showAlert(Activity act, String msg, DialogInterface.OnClickListener listener) {
        AlertDialog.Builder alert = new AlertDialog.Builder(act);
        alert.setMessage(msg);
        alert.setPositiveButton("OK", listener);
        alert.show();
    }

and you can use any class, for example, the code below.

AllMethod.showAlert(mActivity, "", new DialogInterface.OnClickListener() {
        @Override
         public void onClick(DialogInterface dialog, int which) {
         // Do your code for click
         }
});
+7
source

All Articles