0

I'm creating a custom popup using dialog in android. When using custom popup I'm using a cancel button for closing it. But now I want to close it by touching outside of that popup. how to to?

What I've tried till now

private void showDetails() {
    final Dialog dialog = new Dialog(mContext, android.R.style.Theme_Translucent);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.popup_base_menu);
    dialog.setCanceledOnTouchOutside(true);

    text_cancel = (TextView) dialog.findViewById(R.id.text_cancel);
    txt_create_group_chat = (TextView) dialog.findViewById(R.id.txt_create_group_chat);
    txt_create_chat = (TextView) dialog.findViewById(R.id.txt_create_chat);
    txt_create_chatroom = dialog.findViewById(R.id.txt_create_chatroom);

    text_cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();

        }
    });

    txt_create_group_chat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(mContext, CreateGroupChat.class);
            startActivity(in);
            dialog.cancel();
        }
    });
    txt_create_chat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent in = new Intent(mContext, SearchActivity.class);
            startActivity(in);
            dialog.cancel();
        }
    });
    txt_create_chatroom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent in = new Intent(mContext, CreateChatroomActivity.class);
            startActivity(in);
            dialog.cancel();
        }
    });

    dialog.show();

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Tanvir Durlove
  • 768
  • 2
  • 9
  • 21

2 Answers2

0

Use

setCanceledOnTouchOutside(true)

See Android developer documentation.

And create your Dialog like shown here, using an AlertDialog.Builder. This class offers the same constructor parameters as your utilized Dialog.

Additionally, this question already has an answer.

David Artmann
  • 4,272
  • 1
  • 16
  • 24
0

Three changes to be made before showing the Dialog:

 dialog.setCanceledOnTouchOutside(true);
 setCancelable(boolean)

The last thing that may help is to go to the layout popup_base_menu and change the root view height and width to:

 wrap_content

It should instead of match_parent. All this change should be called before dialog.show();

Xenolion
  • 12,035
  • 7
  • 33
  • 48