0

In my application I am showing a custom AlertDialog for the user to enter the a string. When the user presses the positive button, there should be an AsyncTask in order to check the string provided by the user. If the check is successful I want to dismiss() the AlerDialog, on the other hand, if the check fails I want the AlerDialog to stay visible for the user to make the necessary corrections.

The problem is that I have no idea how to avoid the AlerDialog to dismiss after click of any of the buttons (I have checked with all of them).

None of the answers I have found here like this question seem to work.

Here is my piece of code

LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.generic_textinput_dialog, null);

final EditText editText = (EditText) dialogView.findViewById(R.id.generic_dialog_text_editText);
editText.setHint("Username");

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
dialogBuilder.setMessage("Share xxx");
dialogBuilder.setPositiveButton("Share", new DialogInterface.OnClickListener() {

    @Override
    public void onClick (DialogInterface dialog, int which) {

        String sharingUser = editText.getText().toString();
        if (!TextUtils.isEmpty(sharingUser)) {
            doSomething();
        }

        actionMode.finish();
    }
});
dialogBuilder.setNegativeButton("Cancel", null);
dialogBuilder.setView(dialogView);
dialogBuilder.show();

Any idea how to solve this issue?

NOTES

  • Obviously, I am not making use of dialog.dismiss() anywhere an even though the Dialog is dismissed after clicking any of the buttons.
  • I am using API v23.1.1.
Community
  • 1
  • 1
kazbeel
  • 1,378
  • 19
  • 40
  • 3
    If you do not want `AlertDialog` behavior, do not use `AlertDialog`. Create your own `Dialog` (or a dialog-themed activity). – CommonsWare Feb 11 '16 at 19:12
  • I guess this would be the best option. I have spent too much time looking for a solution around the `AlertDialog`. – kazbeel Feb 12 '16 at 06:11

1 Answers1

0

You can always override the button on click listener to avoid the default functionality.

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
dialogBuilder.setMessage("Share xxx");
dialogBuilder.setPositiveButton("Share", null);
dialogBuilder.setNegativeButton("Cancel", null);
dialogBuilder.setView(dialogView);
dialogBuilder.show();
    Button positive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
            positive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        String sharingUser = editText.getText().toString();
                        if (!TextUtils.isEmpty(sharingUser)) {
                          doSomething();
                        }

                        actionMode.finish();
                }
   });
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • This solution is the same as the answer I have mentioned in my question and it doesn't seem to work in my case: even setting the positive and negative button listeners to `null` the AlertDialog is dismissed after a click. – kazbeel Feb 12 '16 at 06:10