-1

I am using AlertDialog builder to take some input from user, where I have written a small validation, like: If EditText is empty show SnackBar and do not close dialog.

What's happening, once user left EditText empty and tap on Positive button, getting message in Snackbar but also closing dialog.

So how can I control on closing of AlertDialog, If condition fails, here is the code:

public void inputDialog() {

        LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context);
        final View mView = layoutInflaterAndroid.inflate(R.layout.layout_dialog, null);
        final AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(context);

        acceptUserInput = (EditText) mView.findViewById(R.id.acceptUserInput);

        alertDialogBuilderUserInput.setView(mView);

        alertDialogBuilderUserInput
                .setCancelable(false)
                .setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogBox, int id) {
                        // ToDo get user input here

                        String strUserInput = acceptUserInput.getText().toString().trim();
                        if(TextUtils.isEmpty(strUserInput)) {

                            Snackbar snackbar = Snackbar.make(mView, "Name field cannot be left blank", Snackbar.LENGTH_LONG);
                            snackbar.show();

                            return;

                        }
                    }
                })

                .setNegativeButton("CLOSE",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialogBox, int id) {
                                dialogBox.cancel();
                            }
                        });

        AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();
        alertDialogAndroid.show();

        }
Sophie
  • 2,594
  • 10
  • 41
  • 75

3 Answers3

0

You can set onShowListener as follows :

View view = getLayoutInflater().inflate(R.layout.layout_dialog, null);

acceptUserInput = (EditText) mView.findViewById(R.id.acceptUserInput);

   final AlertDialog alertDialog = new AlertDialog.Builder(context)
            .setView(view)
            .setCancelable(false)
            .setPositiveButton("SAVE", null)
            .setNegativeButton("CLOSE", null)
            .create();

alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {

            Button buttonPositive = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
            buttonPositive.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                 String strUserInput = acceptUserInput.getText().toString().trim();
                        if(TextUtils.isEmpty(strUserInput)) {

                            Snackbar snackbar = Snackbar.make(mView, "Name field cannot be left blank", Snackbar.LENGTH_LONG);
                            snackbar.show();
                            return;
                        }
            });

            Button buttonNegative = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
            buttonNegative.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                     alertDialog.dismiss();
                }
            });
        }
    });
Suraj Makhija
  • 1,376
  • 8
  • 16
0
AlertDialog.Builder mDialog = new AlertDialog.Builder(
            MapActivity.this);    

 AlertDialog mAlertDialog = mDialog.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

                           mEdt.setText("message"); 
                        }
                    });
                }
            });
            mAlertDialog.show();
Radhey
  • 2,139
  • 2
  • 24
  • 42
0

Working solution as per your question is given below:-

public void inputDialog() {

    LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context);
    final View mView = layoutInflaterAndroid.inflate(R.layout.layout_dialog, null);
    final AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(context);

    final EditText acceptUserInput = (EditText) mView.findViewById(R.id.acceptUserInput);

    alertDialogBuilderUserInput.setView(mView);

    alertDialogBuilderUserInput
            .setCancelable(false)
            .setPositiveButton("SAVE", null)
            .setNegativeButton("CLOSE",null);

    final AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();
    alertDialogAndroid.show();
    alertDialogAndroid.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            String strUserInput = acceptUserInput.getText().toString().trim();
            if(TextUtils.isEmpty(strUserInput)) {

                Snackbar snackbar = Snackbar.make(mView, "Name field cannot be left blank", Snackbar.LENGTH_LONG);
                snackbar.show();
            }
        }
    });
    alertDialogAndroid.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialogAndroid.cancel();
        }
    });
}
Nitin Patel
  • 1,605
  • 13
  • 31