Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 937
sangeeta Avatar asked Feb 05 '23 23:02

sangeeta


2 Answers

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();
            }
        });
like image 125
Murtuza Ali Khan Mohammed Avatar answered Feb 07 '23 11:02

Murtuza Ali Khan Mohammed


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.

like image 42
abhishek Avatar answered Feb 07 '23 12:02

abhishek