Use alertDialog.setPositiveButton and alertDialog.setNegativeButton . Here is a utility method that 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) {
See Android docs for more details.
source share