AlertDialog with OK / Cancel buttons

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 the OK and Cancel buttons. I searched here on StackOverflow, but the setButton method seems deprecated. I also found setPositiveButton and setNegativeButton for AlertDialog.Builder, but even they seem deprecated.

+6
source share
4 answers

You can use AlertDialog.Builder.setPositiveButton and AlertDialog.Builder.setNegativeButton , both are not deprecated ( see 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(); 
+16
source

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) { // OK // do Something } }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Cancel // do Something } }); 

See Android docs for more details.

+3
source
 I usually use this code and It works perfectly use this DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which){ case DialogInterface.BUTTON_POSITIVE: finish(); break; case DialogInterface.BUTTON_NEGATIVE: break; } } }; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Want To Do Some Task").setPositiveButton("Ok"), dialogClickListener) .setNegativeButton("Cancel"), dialogClickListener).show(); 
0
source

try this code, i think it will work fine.

 public void open(View view){ AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setTitle("alert dialog"); alertDialogBuilder.setMessage("conform to delete"); alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Intent positveActivity = new Intent(getApplicationContext(),com.example.alertdialog.PositiveActivity.class); startActivity(positveActivity); } }); alertDialogBuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent negativeActivity = new Intent(getApplicationContext(),com.example.alertdialog.NegativeActivity.class); startActivity(negativeActivity); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } 
0
source

Source: https://habr.com/ru/post/974363/


All Articles