DialogFragment does not occupy bezel less screen
Asked Answered
B

3

7

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

Breda answered 1/12, 2018 at 15:26 Comment(1)
how did you manage to show full screen view for activity ?Grant
K
10

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>
Kaitlynkaitlynn answered 7/12, 2018 at 12:53 Comment(1)
Best working solution! Thank you very muchAnetteaneurin
O
0

In your code, you should include android "windowMinWidthMajor" and "windowMinWidthMinor" properties and set dialog.getWindow() layouts to MATCH_PARENT and make the layout in LinearLayout.

<style name="Theme_Dialog_tab" parent="Theme.AppCompat.Dialog">
        <item name="android:windowMinWidthMajor">100%</item>
        <item name="android:windowMinWidthMinor">100%</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowBackground">@null</item>
    </style>

In Java file:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.Theme_Dialog);

dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

I hope it will help you....!

Orelee answered 6/12, 2018 at 5:39 Comment(1)
Thank you for your help, I tried your solution but it does not work. I am using a Dialog Fragment and not an AlertDialog.Breda
M
0

I found another simple solution, just try to put this code on onCreate() method :

Dialog yourDialog;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    yourDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS );
    yourDialog.getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
Midrash answered 5/5, 2021 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.