Android Don't dismiss AlertDialog after clicking PositiveButton

AndroidAndroid Alertdialog

Android Problem Overview


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();

Android Solutions


Solution 1 - Android

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();

Solution 2 - Android

based on the most voted answer for https://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked

final AlertDialog d = new AlertDialog.Builder(context)
            .setView(v)
            .setTitle(R.string.my_title)
            .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
            .setNegativeButton(android.R.string.cancel, null)
            .create();

    d.setOnShowListener(new DialogInterface.OnShowListener() {
        
        @Override
        public void onShow(DialogInterface dialog) {
            
            Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View view) {
                    // TODO Do something
                    
                }
            });
        }
    });  

I believe you need to override the positive button's handler. Add your logic to dismiss the dialog when a certain condition is met.

Solution 3 - Android

Even Simpler:

final AlertDialog alertDialog = new AlertDialog.Builder(context).setView(v)
                .setPositiveButton(android.R.string.ok, null)
                .setNegativeButton(android.R.string.cancel, null)
                .show();

        Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //Do Your thing
            }
        });

Solution 4 - Android

Answer in Kotlin :

val dialog = AlertDialog.Builder(context)
    .setView(v)
    .setTitle(R.string.my_title)
    .setPositiveButton(android.R.string.ok, null)
    .setNegativeButton(android.R.string.cancel, null)
    .create()

dialog.setOnShowListener {

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
            // Apply logic here
        }

    }

Solution 5 - Android

I do it like this:

    final AlertDialog dialog = new AlertDialog.Builder(this)
            .setCancelable(false)
            .setPositiveButton("YES", null)
            .setNegativeButton("NO", null)
            .show();

    Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    positiveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
       //     Toast.makeText(SysManagerActivity.this, "dialog is open", Toast.LENGTH_SHORT).show();
            
        }
    });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYi-Ying LuView Question on Stackoverflow
Solution 1 - AndroidChitrangView Answer on Stackoverflow
Solution 2 - AndroidAn SO UserView Answer on Stackoverflow
Solution 3 - AndroidM. Usman KhanView Answer on Stackoverflow
Solution 4 - AndroidSteve LukisView Answer on Stackoverflow
Solution 5 - AndroidYury MatatovView Answer on Stackoverflow