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>
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.
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();
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With