Forum message

android messagebox not showing due to call complete how to make this function wait ok and then close

public void msbox(String str,String str2) { AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); dlgAlert.setMessage(str2); dlgAlert.setTitle(str); dlgAlert.setPositiveButton("OK", null); dlgAlert.setCancelable(true); dlgAlert.create().show(); finish(); } 

should be like that

 public void msbox(String str,String str2) { AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); dlgAlert.setTitle(str); dlgAlert.setMessage(str2); dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { finish(); } }); dlgAlert.setCancelable(true); dlgAlert.create().show(); } 
+6
source share
2 answers

see SO question: AlertDialog not waiting for input

you will need to make a callback (OnClickListener) when the user clicks OK on the AlertDialog.

This is all because Android dialogs are not modal (non-blocking call flow)

 dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // call your code here } }); 
+6
source

If you want to create a dialog box (Message Box named in C #, vb.net, etc.) in the Android software, just copy this code and paste it into the click event of any button where you need it.

 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder .setTitle("Deleting a Contact No") .setMessage("Are you sure?") .setIcon(android.R.drawable.ic_dialog_alert) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //do some thing here which you need } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); 
+5
source

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


All Articles