Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Don't dismiss AlertDialog after clicking PositiveButton

Can I just don't dismiss my AlertDialog after clicking PositiveButton?

I would like to remain the dialog to show something update on my ArrayAdapter listWords.

This is my code.

AlertDialog.Builder sayWindows = new AlertDialog.Builder(MapActivity.this);  final EditText saySomething = new EditText(MapActivity.this);  sayWindows.setPositiveButton("ok",             new DialogInterface.OnClickListener() {                 public void onClick(DialogInterface dialog, int which) {                     say = userName + " Says: "+saySomething.getText();                     showPosition.setText(say);                                       }             });  sayWindows.setNegativeButton("cancel",             new DialogInterface.OnClickListener() {                 public void onClick(DialogInterface dialog, int which) {                     dialog.dismiss();                 }             });  sayWindows.setAdapter(listWords, null); sayWindows.setView(saySomething); sayWindows.create().show(); 
like image 590
Yi-Ying Lu Avatar asked Sep 28 '14 16:09

Yi-Ying Lu


People also ask

How do you prevent a dialog from closing when a button is clicked?

AlertDialog dialog = (AlertDialog) getDialog(); dialog. getButton(AlertDialog. BUTTON_POSITIVE). setEnabled(false);

How do I turn off AlertDialog on Android?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

How do you dismiss dialog with click on outside of dialog?

You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.


1 Answers

After looking at @Little Child solution, I try to make this. Let us know if this works for you.

    AlertDialog.Builder sayWindows = new AlertDialog.Builder(             MapActivity.this);     final EditText saySomething = new EditText(MapActivity.this);     sayWindows.setPositiveButton("ok", null);     sayWindows.setNegativeButton("cancel", null);     sayWindows.setAdapter(listWords, null);     sayWindows.setView(saySomething);      final AlertDialog mAlertDialog = sayWindows.create();     mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {          @Override         public void onShow(DialogInterface dialog) {              Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);             b.setOnClickListener(new View.OnClickListener() {                  @Override                 public void onClick(View view) {                     // TODO Do something                    say = userName + " Says: "+saySomething.getText();                    showPosition.setText(say);                  }             });         }     });     mAlertDialog.show(); 
like image 192
Chitrang Avatar answered Sep 28 '22 21:09

Chitrang