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


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



I have implemented an AlertDialog, that works as a message dialog. When the user types the messages, he can click on the button "SEND" or the button "CANCEL". The button "SEND" works fine, but the cancel button doesn't work for some reason.


findViewById(R.id.contactfab).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Frontpage.this);
final AlertDialog dialog = dialogBuilder.create();
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("");
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();
}
});

dialogBuilder.show();
}
});



In order to test whether it is listening on the cancel button, I have tried to make it print a string whenever the button is clicked. The string gets printed, but the dialog doesn't close. Have tried with both dialog.dismiss() and dialog.dismiss()


dialog.dismiss()


dialog.dismiss()



What am I missing?





Yes, I have tried with both dialog.dismiss() and dialog.dismiss()
– Bab
Jun 30 at 14:25





Did any of the answers here help? stackoverflow.com/questions/35554272/… In particular, calling dialogBuilder.create().show()?
– Tyler V
Jun 30 at 14:25



dialogBuilder.create().show()




1 Answer
1



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.


dialogBuilder.show();


AlertDialog


final AlertDialog dialog


dialog


dialog.cancel()


dialog.close()



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();
}
});
}





Thanks, it works! Could you please explain this? I would like to know what was wrong.
– Bab
Jun 30 at 14:39





@Bab check the updated answer
– Sagar
Jun 30 at 14:55






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter