-2

I have fullscreen fragment dialog. While it is showing, background objects can be clickable or interact with user. how do i avoid this?

Here is piece of code how i show dialog:

MainActivity.java

FullScreenFrDialog fr = new FullScreenFrDialog();

FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
tr.add(android.R.id.content, fr, FR_TAG).commit();

FullScreenFrDialog.java

public class FullScreenFrDialog extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog, container, false);
    return view;
}

}

dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCFFFFFF" >

<ImageView
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

</FrameLayout>

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

This button clickable, while dialog is showing.

Thanks in advance.

ssmm
  • 171
  • 1
  • 1
  • 10

2 Answers2

1
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#CCFFFFFF" >

Make the parents clickable true, hope this will help you.

Silvans Solanki
  • 1,267
  • 1
  • 14
  • 27
0

In your onCreate() simply call setCancelable(false). This will prevent the dialog from closing if tapped outside the dialog and will also prevent tap events to the elements beneath

EDIT

Alternatively you can call setCanceledOnTouchOutside(false) if you want it to be dismissed with a back button press but still prevent touches to elements beneath

camelCaseCoder
  • 1,447
  • 19
  • 32