Last active
December 25, 2024 06:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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
Where
isDark
come from?