show alertdialog in android

show alertdialog in android

// 1. Instantiate an AlertDialog.Builder

refer to‮iruttual:‬.com
AlertDialog.Builder builder = new AlertDialog.Builder(context);

// 2. Call various setter methods to set the dialog characteristics

builder.setTitle("My Title");
builder.setMessage("My Message");

// builder.setPositiveButton("OK", (dialog, which) -> { /* ... */ });
builder.setPositiveButton(getResources().getString(R.string.sure), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(AccountActivity.this, R.string.log_out_success,
                Toast.LENGTH_LONG).show();
       
    }
});

// builder.setNegativeButton("Cancel", (dialog, which) -> { /* ... */ });
builder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});

// 3. Instantiate the AlertDialog

AlertDialog dialog = builder.create();

// 4. Show the AlertDialog

dialog.show();
Created Time:2017-08-30 12:04:32  Author:lautturi