Fabrizio Ferrari
Fabrizio Ferrari

Reputation: 989

How to hide bottom navigation bar in fragment when dialog is displayed in Kotlin?

I am having a hard time hiding the bottom navigation bar when a dialog is displayed. I want that to NOT appear since the application is shown in full-screen mode.

Here is the class that builds the dialog:

class CreateNewFolderDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        return activity?.let {
            // Use the Builder class for convenient dialog construction
            //val builder = AlertDialog.Builder(it, R.style.AppBaseTheme)
            val builder = AlertDialog.Builder(it)
            val inflater = requireActivity().layoutInflater;

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(inflater.inflate(R.layout.create_new_folder_dialog, null))
                    // Add action buttons
                    .setPositiveButton("OK",
                            DialogInterface.OnClickListener { dialog, id ->
                                // create folder ...
                            })
                    .setNegativeButton("Cancel",
                            DialogInterface.OnClickListener { dialog, id ->
                                getDialog()?.cancel()
                            })
            builder.create()


        } ?: throw IllegalStateException("Activity cannot be null")
    }
}

And here is the call of it inside another fragment:

fun createNewFolderDialog() {

        val supportFragmentManager: FragmentManager = (activity as AppCompatActivity).supportFragmentManager

        val newFragment = CreateNewFolderDialogFragment()

        newFragment.show(supportFragmentManager, "newfolder")


}

And here is a screenshot of what I get when I show the dialog. As you can see the bottom navigation bar is shown. I'd like to prevent that to appear, ever.

Bottom bar shown when dialog appears

Upvotes: 1

Views: 1839

Answers (2)

Fabrizio Ferrari
Fabrizio Ferrari

Reputation: 989

I solved the problem with this:

override fun onStart() {
    super.onStart()
    dialog?.window?.decorView?.apply {
        systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE or
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    SYSTEM_UI_FLAG_HIDE_NAVIGATION
    }
}

Thanks to @Nikhil for the provided hints!

Upvotes: 1

Nikhil Jain
Nikhil Jain

Reputation: 648

You can hide the system navigation bar through the following code :

window.decorView.apply {
    // Hides the navigation bar.
    systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
}

There are few limitations to hide the system navigation bar, you can check this on the below link. Source - Android Documentation

You can use the window object of the DialogFragment class and can set the above flag in your CreateNewFolderDialogFragment class in onStart method like the code below:

override fun onStart() {
    super.onStart()
    dialog?.window?.decorView?.apply {
        // Hide both the navigation bar
        systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    }
}

Upvotes: 1

Related Questions