sangeeta
sangeeta

Reputation: 158

How to dismiss a popup in Android?

I have a Popup window that pops up when a button in an activity is clicked. The dialog covers the whole screen and contains only three buttons. If a button is clicked, then they function accordingly. However, if a user touches anywhere in a popup other than buttons, I want the popup to dismiss. I tried the following code.

popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(layout, Gravity.FILL, 0, 0);

popupWindow.setTouchInterceptor(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            popupWindow.dismiss();
            return true;
        }
        return false;
    }
});

But it didn't work. I also set this:

popupWindow.setFocusable(true);

popupWindow.setTouchable(true);

My popup layout looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10px">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="10dp">

        <ImageButton
            android:id="@+id/voiceCall"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center|center_horizontal"
            android:background="@drawable/round_button"
            android:padding="15dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_call_black_24dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Voice Call"
            android:textColor="#ffffff"
            android:textSize="20dp" />
    </LinearLayout>
</LinearLayout>

enter image description here

If I click anywhere on this popup except for those two buttons, I want it to be dismissed. Nothing works for me. Can anyone suggest me how to do this? Also, I want the popup to disappear if a device back button is clicked.

Upvotes: 4

Views: 10431

Answers (7)

abhishek
abhishek

Reputation: 88

Here is the answer -

just write like this below:

popupWindow.setOutsideTouchable(true);
popupWindow.setCancelable(true);

it will work fine! Please vote if answer is helpful.

Upvotes: 2

Kuldeep Rathee
Kuldeep Rathee

Reputation: 290

Firstly make your Layout width and height to wrap_content so that you can view the remaining screen. then you can enable the setCancelable() attribute as true like popupWindow.setCancelable(true);

or you can use popupWindow.dismiss();

inside your onBackPress() method which is to be Override ex:-

@Override
public void onBackPressed() {
    super.onBackPressed();
    popupWindow.dismiss();
}

Upvotes: 0

Alan
Alan

Reputation: 296

Do you mean:

popupWindow.setCancelable(true);

or

popupWindow.setCanceledOnTouchOutside(true);

depends on builder level or dialog level

@ update2:

// set a listener to listen your popup window click event
popupWindow.getContentView().setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        popupWindow.dismiss();
    }
});

If back btn is clicked, check whether it is showing:

@Override
public void onBackPressed() {
    if(popupWindow.isShowing()) {
        popupWindow.dismiss();
        return;
    }
    super.onBackPressed();
}

Upvotes: 1

user7462841
user7462841

Reputation:

Try this code snippet in the activity where your popup appear:

@Override
    public void onBackPressed() {
        super.onBackPressed();
        popupWindow.setOutsideTouchable(true); 
        popupWindow.dismiss();

}

Upvotes: 0

You can write a setOnClick listener for the linearlayout in which the two buttons are present. Like this...

linearlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

Upvotes: 5

Mayura Savaliya
Mayura Savaliya

Reputation: 440

If popupWindow is object of an AlertDialog, you can use

dialog.setCanceledOnTouchOutside(true);

Upvotes: 0

Chirag.T
Chirag.T

Reputation: 756

Just use popupWindow.dismiss(); why you use setTouchInterceptor and all that..

Upvotes: 0

Related Questions