Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment does not occupy bezel less screen

I am trying to display a dialog fragment in a bezeless phone which has a notch. Here is the screenshot.

enter image description here

As you can see the dialog fragment does not occupies the whole screen and show an ugly grey color at the top.

Here is what i have tried

I am setting the style in DialogFragment

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialogStyle)
    }



<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>

I am using the same technique for Activity Screen and it works as it occupies the whole bezelless screen but this does not work for dialog fragment

like image 802
Pritish Avatar asked Dec 01 '18 15:12

Pritish


People also ask

Is DialogFragment deprecated?

Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.

What is the difference between dialog and DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do I know if DialogFragment is showing?

Showing the DialogFragment Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.


1 Answers

You have two options here. First, let's look at the components that are common to both options;

DialogFragment's onStart method:

override fun onStart() {
    super.onStart()
    val dialog: Dialog? = dialog
    if (dialog != null) {
        val width = ViewGroup.LayoutParams.MATCH_PARENT
        val height = ViewGroup.LayoutParams.MATCH_PARENT
        dialog.window?.setLayout(width, height)
        dialog.window?.decorView?.systemUiVisibility =
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_FULLSCREEN
    }
}

DialogFragment's onCreate method:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialogStyle)
    }

Dialog xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#c92145">

    <Button android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Lorem ipsum dolor sit amet"/>

</LinearLayout>

FullScreenDialogStyle:

<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>

Options: options

Left (Option #1)

Add this line to FullScreenDialogStyle

<item name="android:fitsSystemWindows">true</item>

Right (Option #2)

Add this line to FullScreenDialogStyle

<item name="android:fitsSystemWindows">false</item>
like image 121
Anıl Furkan Ökçün Avatar answered Oct 12 '22 10:10

Anıl Furkan Ökçün