1

I have an AlertDialog with a view as shown below.

userEmail = new EditText(mActivity);
userEmail.setSingleLine(true);

userEmail.setHint("Email address");
userEmail.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
userEmail.addTextChangedListener(this);

AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setTitle(getResources().getString(R.string.text_dialog_email));
builder.setView(userEmail);

I have added a POSITIVE button to it with an event listener.

builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
          if(userEmail.getText().toString().trim().length() <= 0) {
                userEmail.setError("Email address can't be empty");
          } else {
                  //some logic

                 InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
                      imm.hideSoftInputFromWindow(userEmail.getWindowToken(), 0);

                 dialog.dismiss(); 
           }

} });

builder.create();
builder.show();

There is an email validation when user hits ok button. I was expecting the dialog to stay if the Edittext is empty. However, in real, dialog disappears even if the EditText is empty.

Is there anything I am lacking?

EDIT

The whole code is inside a method, openEmailDialog(). It is invoked on a buttonclick.

@Override
public void onClick(View view) {
   openEmailDialog();
}
Renjith
  • 3,457
  • 5
  • 46
  • 67

2 Answers2

0

If you add the onClickListener via the builder, the dialog will automatically close.

You can prevent this, by manually adding the View.OnClickListener to the Button view of the dialog. You can receive the Button View from the dialog, after it has been shown.

You should try this:

builder.setPositiveButton(android.R.string.ok, null);//init button
final AlertDialog d = builder.create();//store reference to the dialog
//the click listener for your button
final View.OnClickListener myListener = new View.OnClickListener(){
    public void onClick(View v){
         if(userEmail.getText().toString().trim().length() <= 0) {
            userEmail.setError("Email address can't be empty");
      } else {
              //some logic

             InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(userEmail.getWindowToken(), 0);

             d.dismiss(); 
       }
    }
});

d.setOnShowListener(new DialogInterface.OnShowListener(){

    public void onShow(DialogInterface dialog){
        //here get the Button and set onclicklistener
        Button b = d.getButton(DialogInterface.BUTTON_POSITIVE);
        b.setOnClickListener(myListener);//your validation in your onclicklistener.
        //do not forget to dismiss the dialon in your View.OnClickListener
    } 

});
d.show();
Sipka
  • 2,291
  • 2
  • 27
  • 30
0

You may solve this problem by using Java Reflection.

 private void canCloseDialog(DialogInterface dialogInterface, boolean close) {  
     try {  
         Field field = dialogInterface.getClass().getSuperclass().getDeclaredField("mShowing");

         field.setAccessible(true);  
         field.set(dialogInterface, close);  
     } catch (Exception e) {  
         e.printStackTrace();  
     }  
 }

Call this method at the onClick callback

 new AlertDialog.Builder(this)  
         .setTitle(R.string.abc)  
         .setView(passwdEditText)  
         .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {  
             @Override  
             public void onClick(DialogInterface dialogInterface, int i) {  
                 canCloseDialog(dialogInterface, false);//will NOT close the Dialog
             }  
         })  
         .setNeutralButton(android.R.string.cancel, new DialogInterface.OnClickListener() {  
             @Override  
             public void onClick(DialogInterface dialogInterface, int i) {  

                 canCloseDialog(dialogInterface, true);//will close the Dialog
             }  
         })  
         .show();
Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
Lei
  • 22
  • 3