Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save diegohkd/f7199ffd324222bd8bff215c601eb553 to your computer and use it in GitHub Desktop.
Save diegohkd/f7199ffd324222bd8bff215c601eb553 to your computer and use it in GitHub Desktop.
Show DialogFragment in full screen with status bar, change status bar background color and update status bar components color depending on background color to make them more readable
...
import androidx.fragment.app.DialogFragment
...
class MyDialogFragmentDialog : DialogFragment() {
...
private val statusBarColorRes = R.color.colorPrimaryDark
private var previousSystemUiVisibility: Int? = null
...
override fun getTheme(): Int = R.style.FullscreenDialog
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupStatusBarColor()
}
...
override fun onDestroy() {
previousSystemUiVisibility?.let(::setActivitySystemUiVisibility)
super.onDestroy()
}
...
private fun setupStatusBarColor() {
previousSystemUiVisibility = getActivitySystemUiVisibility()
setStatusBarColor(statusBarColorRes)
}
}
val DialogFragment.window: Window? get() = dialog?.window
val DialogFragment.activityDecorView: View? get() = activity?.window?.decorView
fun DialogFragment.getActivitySystemUiVisibility(): Int? =
window?.decorView?.systemUiVisibility
fun DialogFragment.setActivitySystemUiVisibility(flags: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activityDecorView?.systemUiVisibility = flags
}
}
fun DialogFragment.setStatusBarColor(@ColorRes colorRes: Int) {
window?.run {
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
val color = ContextCompat.getColor(context, colorRes)
statusBarColor = color
// does not work if dim is enabled
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val decorView = activityDecorView ?: return
if (!color.isDark) {
decorView.systemUiVisibility =
decorView.systemUiVisibility or SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
}
}
<style name="NoMarginsDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment