AlertDialog with OK/Cancel buttons

14,698

Solution 1

You can use AlertDialog.Builder.setPositiveButton and AlertDialog.Builder.setNegativeButton, both are not deprecated (see the documentation):

new AlertDialog.Builder(HomePage.this)
        .setTitle("Timeout connessione")
        .setMessage("Connessione lenta o non funzionante")
        .setNegativeButton(android.R.string.cancel, null) // dismisses by default
        .setPositiveButton(android.R.string.ok, new OnClickListener() {
            @Override public void onClick(DialogInterface dialog, int which) {
                // do the acknowledged action, beware, this is run on UI thread
            }
        })
        .create()
        .show();

Solution 2

Use alertDialog.setPositiveButton and alertDialog.setNegativeButton, Here is a Utility Method you can use:

public static void ask(final Activity activity, String title, String msg,
                       DialogInterface.OnClickListener okListener, 
                       DialogInterface.OnClickListener cancelListener) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
    alertDialog.setTitle(title);
    alertDialog.setMessage(msg);
    alertDialog.setPositiveButton(R.string.ok, okListener);
    alertDialog.setNegativeButton(R.string.cancel, cancelListener);
    alertDialog.show();
}

You can call it like this:

ask(mInstance, getString(R.string.app_name),
            getString(R.string.confirm_text_here),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) { // OK
                    // do Something
                }
            }, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) { // Cancel
                    // do Something
                }
            });

For more details refer Android Docs

Share:
14,698
smartmouse
Author by

smartmouse

Considerable experience in web applications development, both as front-end developer and as CMS webmaster. Bitcoin and blockchain enthusiast as writer, speaker and developer of personal projects. An effective communicator with good leadership and analytical skills. Seeking new challenges and responsibilities to progress career. Spare time is for reading news, traveling and working on new ideas...

Updated on July 02, 2022

Comments

  • smartmouse
    smartmouse almost 2 years

    I create this AlertDialog:

                String msg = "Connessione lenta o non funzionante";
                AlertDialog alertDialog;
                alertDialog = new AlertDialog.Builder(HomePage.this).create();
                alertDialog.setTitle("Timeout connessione");
                alertDialog.setMessage(msg);
                alertDialog.show();
    

    I want to add OK and Cancel buttons. I searched here on StackOverflow but setButton method seems to be deprecated. I also found setPositiveButton and setNegativeButton for AlertDialog.Builder but even them seem to be deprecated.