Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: Dialog, cancel button

Tags:

android

dialog

I have created a custom dialog, And I have inserted two buttons, one is called send, and the other cancel.

I would like to know hot to close down the dialog when pressing cancel?

like image 591
Troj Avatar asked Dec 08 '22 01:12

Troj


2 Answers

Are you using the AlertDialog.Builder? There are lots of way of implementing a custom Dialog but assuming your "Send" button is a positive or neutral button and your "Cancel" button is a negative button you can always do this.

@Override
public void onClick(DialogInterface dialog, int which) {
    if (which == Dialog.BUTTON_NEGATIVE)
        dialog.dismiss()
}

You can also call dialog.cancel() if you want to invoke the DialogInterface.OnCancelListener.

If this is not the case you need to supply additional information.

like image 110
glr Avatar answered Dec 23 '22 20:12

glr


try this

dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");


Button cancle_btn = (Button) dialog.findViewById(R.id.cancle_btn);
dialog_btn.setOnClickListener(new View.OnClickListener() 
{
    // Perform button logic
    dialog.dismiss();
}
like image 32
Chirag Patel Avatar answered Dec 23 '22 18:12

Chirag Patel