0

I have alert dialog with edit text. User must enter something and if not and then system should alert him that this input is required.

This is code for Alter Dialog:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Razlog storniranja?");

    // Set up the input
    final EditText input = new EditText(this);
    // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
    input.setInputType(InputType.TYPE_CLASS_TEXT);
    input.setSingleLine(false);
    input.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
    builder.setView(input);

    // Set up the buttons
    builder.setPositiveButton("Spremi", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.setNegativeButton("Otkaži", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {

            if (m_Text.length()==0)
            {
                input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
                input.setError("Upišite razlog storniranja!");

            }

        }
    });

    builder.show();

But when user click on positive button and input is empty the dialog is closed. Is something missing in my code to avoid closing dialog?

EDIT

I think I found solution. The last line builder.show(); should be deleted and instead, this piece of code should be added:

    builder.setView(input);
    final AlertDialog alertDialog = builder.create();
    alertDialog.show();

    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Boolean wantToCloseDialog = (input.getText().toString().trim().isEmpty());
            // if EditText is empty disable closing on possitive button
            if (!wantToCloseDialog) {
                input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
                input.setError("Upišite razlog storniranja!");
                alertDialog.dismiss();
            }
        }
    });

I hope this will help others.

Josef
  • 2,648
  • 5
  • 37
  • 73
  • Possible duplicate of [AlertDialog with positive button and validating custom EditText](https://stackoverflow.com/questions/11363209/alertdialog-with-positive-button-and-validating-custom-edittext) – Tyler Kindy May 23 '17 at 19:01

2 Answers2

1

Well, I do not have any clue of what you are trying to do.

The error that I had when using your code was:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

m_Text needs to be initialized.

in case m_Text is not initialized, on the onDismiss method you might need to change:

if (m_Text != null && m_Text.length()==0){ //this line
     input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
     input.setError("Upišite razlog storniranja!");
}

Regarding the cancelable, you need to builder.cancelable(false); and whenever needed, builder.dismiss().

builder.setPositiveButton("Spremi", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            m_Text = input.getText().toString();

            if (m_Text.isEmpty()) {
                input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
                input.setError("Upišite razlog storniranja!");
                input.setFocusable(true);
            }else{
                dialog.dismiss();
            }
        }
    });
Amg91
  • 165
  • 8
  • 25
1

Use TextUtils.isEmpty() to check the empty string.

Try this:

builder.setPositiveButton("Spremi", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        if(TextUtils.isEmpty(input.getText().toString())) {
            // Empty
            input.setError("Input is required!");
        } else {
            // Do something...
        }
    }
});

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61