0

I have been trying to make a fragment fullscreen, but almost every answer on the web has deprecated methods. Even the android official site has a deprecated method Link.

I'm using kotlin and after following this answer, I have tried this in fragment.

override fun onAttach(context: Context) {
    super.onAttach(context)
    requireActivity().window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}

override fun onDetach() {
    super.onDetach()
    requireActivity().window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}

but the result that I got is this

enter image description here

You can clearly see navigation and status bars are still there.

Can you share the proper and latest way to get fullscreen in fragment?

kallis
  • 83
  • 3
  • 18

3 Answers3

1

If you read the docs for ViewCompat.getWindowInsetsController(), you'll see that it's only deprecated because they want you to use getInsetsController() instead. Otherwise, the sample code works just fine.

user496854
  • 6,461
  • 10
  • 47
  • 84
1

Can you use the theme as follows

    <style name="AppTheme"parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    </style>
Adin D
  • 61
  • 4
0

For making a fragment full-screen.

override fun onAttach(context: Context) {
    super.onAttach(context)
    WindowInsetsControllerCompat(requireActivity().window, requireActivity().window.decorView).apply { 
        
        // Hide both the status bar and the navigation bar
        hide(WindowInsetsCompat.Type.systemBars())
        
        // Behavior of system bars 
        systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }
}

Make sure to add this code in your fragment as well, it will show the system bars again.

override fun onDetach() {
    super.onDetach()
    WindowInsetsControllerCompat(requireActivity().window, requireActivity().window.decorView)
        .show(WindowInsetsCompat.Type.systemBars())
}

I don't know how WindowInsetsControllerCompat works but if anyone wants to hide system bars then this will help.

kallis
  • 83
  • 3
  • 18