1

I want my alert dialog box to trigger and stay showing an error message if the user leaves the name field empty and clicks ok but my dialogue box disappears even if the user does not fill anything and click ok.Here is my code. Please suggest me the corrections I need to make.

    save2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final dbmanager db= new  dbmanager(cgpa3.this);

                    final AlertDialog.Builder alert = new AlertDialog.Builder(cgpa3.this);

//                            alert.setTitle("Enter a name");
                alert.setMessage("Enter student Name");

// Set an EditText view to get user input
                final EditText input = new EditText(cgpa3.this);
                alert.setView(input);


                    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            String value = input.getText().toString();
                            if(value.isEmpty()){
                                Animation shake = AnimationUtils.loadAnimation(cgpa3.this, R.anim.shake);
                                input.startAnimation(shake);
                                input.setError("Please enter student name");

                            }
                            else
                            {db.addRecord1(value,textView39.getText(),textView40.getText(),no_of_sem);

                            }

                        }
                    });

                    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            // Canceled.
                        }
                    });

                    alert.show();

            };

        });
Giorgos Neokleous
  • 1,709
  • 1
  • 14
  • 25
  • 1
    Use .setCancelable(false) for your dialog box. It won't disappear. Unless you call dismiss – Attiq ur Rehman Jan 17 '20 at 10:31
  • Does this answer your question https://stackoverflow.com/questions/40261250/validation-on-edittext-in-alertdialog – Volkan Albayrak Jan 17 '20 at 10:34
  • Possible duplicate of https://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked – Khodor Jan 17 '20 at 10:34
  • Does this answer your question? [Validation on EditText in alertDialog](https://stackoverflow.com/questions/40261250/validation-on-edittext-in-alertdialog) – Volkan Albayrak Jan 17 '20 at 10:35

2 Answers2

0

use this one,

       final EditText editText;

            final AlertDialog.Builder alert = new AlertDialog.Builder(DemoActivity.this);
                       alert.setTitle("Enter a name");
             alert.setMessage("Enter student Name");
            alert.setCancelable(false);


            editText = new EditText(DemoActivity.this);
            alert.setView(editText);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });

            final AlertDialog dialogs  = alert.create();
            dialogs.show();

            dialogs.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String value = editText.getText().toString();
                    if (value.isEmpty()) {
                        editText.setError("Please enter student name");
                    }
                    else{
                        dialogs.dismiss();
                    }

                }
            });

            dialogs.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    dialogs.dismiss();
                }
            });
Avinash
  • 867
  • 4
  • 11
0
if(TextUtils.isEmpty(input.getText().toString().trim())){
 Animation shake = AnimationUtils.loadAnimation(cgpa3.this, R.anim.shake);
                            input.startAnimation(shake);
                            input.setError("Please enter student name");
}
else{
db.addRecord1(value,textView39.getText(),textView40.getText(),no_of_sem);
}

if working plese aprove

Kukadiya Anil
  • 116
  • 2
  • 7