0

I'm using the BottomSheetDialogFragment and I'm overriding this to add my theme :

override fun getTheme() = R.style.BottomSheetDialogTheme

Where this style looks like :

<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
  <item name="bottomSheetStyle">@style/btmsheetStyle</item>
  <item name="android:navigationBarColor">@color/white</item>
</style>

<style name="btmsheetStyle" parent="Widget.Design.BottomSheet.Modal">
  <item name="android:backbround">@drawable/rounded_dialog</item>
</style>

My idea is to have the BottomSheetDialogFragment as the right image but is showing as the left one enter image description here

I ended up doing a hack to make it work but the problem is that I'm using a deprecated method and perhaps in Android R it crashes, so I would like to make it better.

This is how I ended up doing the white nav bar android

@RequiresApi(api = Build.VERSION_CODES.M)
private fun setWhiteNavigationBar(dialog: Dialog) {
   val window = dialog.window
   window?.let {
      val displayMetrics = DisplayMetrics()
      it.windowManager.defaultDisplay.getMetrics(displayMetrics) <-- deprecated
      val navigationBarDrawable = GradientDrawable()
      navigationBarDrawable.shape = GradientDrawable.RECTANGLE
      navigationBarDrawable.setColor(Color.WHITE)
      val layers = arrayOf(navigationBarDrawable)
      val windowBackground = LayerDrawable(layer)
      windowBackground.setLayerInsetTop(1, displayMetrics.heightPixels)
      it.setBackgroundDrawable(windowBackground)
   }
}

override fun onCreateDialog(savedInstanceState: Bunde?): Dialog {
   val dialog = super.onCreateDialog(savedInstanceState)
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
      setWhiteNavigationBar(dialog)
   }
   return dialog
}

It does work, but I don't like the deprecated stuff, is there any way to make it better? With style, and avoid having overriding creating dialogs etc?

StuartDTO
  • 783
  • 7
  • 26
  • 72

1 Answers1

1

To avoid deprecation you can use this method and the rest leave like you were doing:

@RequiresApi(api = Build.VERSION_CODES.M)
private fun setWhiteNavigationBar(dialog: Dialog) {
        val window = dialog.window
        window?.let {
            val displayMetrics = getDisplayMetrics(it)
            val dimDrawable = GradientDrawable()
            val navigationBarDrawable = GradientDrawable()
            navigationBarDrawable.shape = GradientDrawable.RECTANGLE
            navigationBarDrawable.setColor(Color.WHITE)
            val layers = arrayOf(dimDrawable, navigationBarDrawable)
            val windowBackground = LayerDrawable(layers)
            windowBackground.setLayerInsetTop(1, displayMetrics.heightPixels)
            it.setBackgroundDrawable(windowBackground)
        }
    }

private fun getDisplayMetrics(window: Window): DisplayMetrics {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val defaultDisplay =
                DisplayManagerCompat.getInstance(requireContext())
                    .getDisplay(Display.DEFAULT_DISPLAY)
            val displayContext = requireContext().createDisplayContext(defaultDisplay!!)
            displayContext.resources.displayMetrics
        } else {
            val displayMetrics = DisplayMetrics()
            @Suppress("DEPRECATION")
            window.windowManager.defaultDisplay.getMetrics(displayMetrics)
            displayMetrics
        }
    }

More info in Display Manager Compat

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148