Alertdialog Dialog.Dismiss(), Dialog.Close() Not Working

Android: AlertDialog not closing on Cancel() nor on Dismiss()

When you call dialogBuilder.show(); it will create a new AlertDialog with arguments provided to the builder and immediately display the dialog. As a result, this new AlertDialog will be different from one declared using final AlertDialog dialog. In short with your code dialog declared will never be shown and hence dialog.cancel() or dialog.close() won't have any impact.

Change your code as follows:

findViewById(R.id.contactfab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Frontpage.this);

LayoutInflater inflater = Frontpage.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_contact, null);
dialogBuilder.setView(dialogView);
final EditText editText = (EditText) dialogView.findViewById(R.id.contactText);
editText.setText("");
final AlertDialog dialog = dialogBuilder.create();
dialogView.findViewById(R.id.send_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String [] reciever = new String[]{"test@hotmail.com"};
String subject = ("Feedback");
Intent mailIntent = new Intent(Intent.ACTION_SEND);
mailIntent.putExtra(Intent.EXTRA_EMAIL, reciever);
mailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
mailIntent.putExtra(Intent.EXTRA_TEXT, editText.getText().toString());
mailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(mailIntent, "Vælg en applikation til at sende din mail med"));
}
});

dialogView.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
dialog.cancel();
}
});

dialog.show();
}
});
}

AlertDialog does not dismiss, takes twice tap to close

You don't need to explicitly dismiss the alert dialog by calling dialog.dismiss(); inside the onClick method of the dialog interface buttons.

The dialog will automatically be closed once you click any of those buttons.

If you're telling the dialog isn't gone after you click the button, you might have created multiple dialogs so that even one dialog is dismissed, another one is still there.

AlertDialog.dismiss() not working

It isn't dismissing because the AlertDialog you're calling AlertDialog.dismiss on isn't the same one that's shown. In other words, you're calling alert.show() and using dialog.dismiss(). To fix it call dialog.show().

Android - why would Dialog not close upon dialog.dismiss()

Make sure that when you are executing dialog.dismiss that it is pointing to the dialog you created. You have dialog as a class variable and there is a high chance it was assigned another dialog by the time of dismissal come. I had this one and turned out that dialog variable at dismiss time was no longer pointing to my actually dialog.

Putting a break point and seeing if the dialog variable upon creation/execution is still the same will probably help

Custom Alert Dialog does not dismiss

You should store the returned dialog into a variable otherwise all you are doing is creating a new AlertDialog instance and calling show() and then another new instance and calling dismiss() (hence the one never dissappears):

AlertDialog dialog = new LoadingDialog(context).LoadDialog();

Then you can call:

dialog.show();

Or

dialog.dismiss();

Not able to close app from Dialog in android

Firstly in your dialog class pass the context of the caller activities say MainActivit.class context

Now first close the dialog

//so as to avoid the window leaks as on destroying the activity it's context would also get vanished.

 dialog.dismiss();

and then

((Activity) context).finish();

Displaying one alert dialog, dismissing and displaying another alert dialog

Okay i don't recommend it to be like this i prefer it to be cleaner as @Tamir expressed in his answer however here is what you need to do so you can refactor later on for cleaner and simpler code to help you take apart and debug easily.

As i explained in the comment here are the amends you will alter
Regarding showVehicleDetails :

AlertDialog.Builder alert_dialog = new AlertDialog.Builder(this);
alert_dialog.setTitle("VEHICLE DETAILS");
alert_dialog.setMessage("Please, fill in the following:");
LayoutInflater inflate = LayoutInflater.from(this);
View vehicle_details = inflate.inflate(R.layout.layout_vehicle_details, null);

final MaterialEditText edtYear = vehicle_details.findViewById(R.id.year);
final MaterialEditText edtCMNM = vehicle_details.findViewById(R.id.cmnm);
final MaterialEditText edtPlate = vehicle_details.findViewById(R.id.plate);

alert_dialog.setView(vehicle_details);

alert_dialog.setPositiveButton("SUBMIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {


// check validation
if (TextUtils.isEmpty(edtYear.getText().toString())) {
Snackbar.make(rootLayout, "Please enter year of your vehicle", Snackbar
.LENGTH_SHORT).show();

return;
}

if (TextUtils.isEmpty(edtCMNM.getText().toString())) {
Snackbar.make(rootLayout, "Please enter the Color, Make and Model of your vehicle",
Snackbar.LENGTH_SHORT).show();

return;
}

if (TextUtils.isEmpty(edtPlate.getText().toString())) {
Snackbar.make(rootLayout, "Please enter your license plate number", Snackbar
.LENGTH_SHORT).show();

return;
}

// save to firebase/ Users/Drivers ...

details = users;
details.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
// get drivers id
for (DataSnapshot ss : snapshot.getChildren()) {
String id = ss.getKey();

String year = edtYear.getText().toString();
String cmnm = edtCMNM.getText().toString();
String plate = edtPlate.getText().toString();

Log.e(TAG, "year: " + year + ", cmnm: " + cmnm + ", plate: " + plate);

// data sent
details.child(id).child("year").setValue(year);
details.child(id).child("cmnm").setValue(cmnm);
details.child(id).child("plate").setValue(plate);
}
showRegisterDialog();
dialog.dismiss();

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}
});
AlertDialog dialog = alert_dialog.create();
dialog.show();}

and here are the amends you will alter
Regarding showRegisterDialogue :

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("REGISTER");
builder.setMessage("Please use email to register");
LayoutInflater inflater = LayoutInflater.from(this);
View register_layout = inflater.inflate(R.layout.layout_register, null);

final MaterialEditText edtEmail = register_layout.findViewById(R.id.email);
final MaterialEditText edtPassword = register_layout.findViewById(R.id.password);
final MaterialEditText edtName = register_layout.findViewById(R.id.usersname);
final MaterialEditText edtPhone = register_layout.findViewById(R.id.cell);

final MaterialAnimatedSwitch policies_switch = register_layout.findViewById(R.id.policies_switch);

builder.setView(register_layout);

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

// check validation
if (TextUtils.isEmpty(edtEmail.getText().toString())) {
Snackbar.make(rootLayout, "Please enter email address", Snackbar
.LENGTH_SHORT).show();

return;
}

if (TextUtils.isEmpty(edtPhone.getText().toString())) {
Snackbar.make(rootLayout, "Please enter phone number", Snackbar
.LENGTH_SHORT).show();

return;
}

if (TextUtils.isEmpty(edtPassword.getText().toString())) {
Snackbar.make(rootLayout, "Please enter password", Snackbar
.LENGTH_SHORT).show();

return;
}

if (edtPassword.getText().toString().length() < 6) {
Snackbar.make(rootLayout, "Password too short !!!", Snackbar
.LENGTH_SHORT).show();

return;
}


// Register new user
auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),
edtPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
// Save user to db
Driver driver = new Driver();
driver.setEmail(edtEmail.getText().toString());
driver.setUsersname(edtName.getText().toString());
driver.setCell(edtPhone.getText().toString());
driver.setPassword(edtPassword.getText().toString());
driver.setRates("4.5"); // TODO: Default added

// use uid to key
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(driver)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout, "Registration successful !!!",
Snackbar.LENGTH_SHORT).show();
dialog.dismiss();
}
})

.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout, "Failed" + e.getMessage(),
Snackbar.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
})

.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout, "Failed" + e.getMessage(),
Snackbar.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
});

builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

final AlertDialog dialog = builder.create();

/* If switch is on, enable REGISTER button */
policies_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean isChecked) {
if (isChecked) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); // if checked, enable
}
}
});

dialog.show();

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); // set disabled by default

}

and for The ClickListener you will just remove the showRegisterDialogue and it would be like this

btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showVehicleDetails();
}
});


Related Topics



Leave a reply



Submit