Peter Sankauskas Peter Sankauskas - 6 months ago 30
Android Question

What's the best was to have reusable dialog boxes?

What's the best was to create reusable dialog boxes in Android?

Reading through the Dialog dev guide, I know I can use

AlertDialog.Builder(this);
in one of my
Activity
s, but what if I want to use this in multiple Activities? If this was some other class I would extend it, so
MyDialog extends AlertDialog
, but then I cannot use the
Builder
.

Any suggestions?

Answer

Crate one class file like as AllMethod.java and add this code in that class file.

public static void showAlert(Activity act,String msg)
        {
            AlertDialog.Builder alert = new AlertDialog.Builder(act);
            alert.setMessage(msg).setPositiveButton("OK", new OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which)
                {   
                }
            }).show();
        }

and you can use from any class like as

AllMethod.showAlert(Activity,"Message");
Comments