Created
June 8, 2019 14:56
-
-
Save Namolem/70930180a4c366f235b9e0f419e0c570 to your computer and use it in GitHub Desktop.
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
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setTheme(R.style.AppTheme) | |
val systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | |
window.decorView.systemUiVisibility = systemUiVisibility | |
setContentView(R.layout.activity_main) | |
someBottomView.doOnApplyWindowInsets { view, windowInsets, initialPadding -> | |
view.setPadding( | |
initialPadding.left, | |
initialPadding.top, | |
initialPadding.right, | |
initialPadding.bottom + windowInsets.systemWindowInsetBottom | |
) | |
} | |
} | |
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="AppTheme" parent="AppTheme.Base"> | |
<item name="android:windowTranslucentStatus">false</item> | |
<item name="android:windowTranslucentNavigation">false</item> | |
<item name="android:windowDrawsSystemBarBackgrounds">true</item> | |
<item name="android:statusBarColor">@android:color/transparent</item> | |
<item name="android:navigationBarColor">@android:color/transparent</item> | |
</style> |
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
fun View.requestApplyInsetsWhenAttached() { | |
if (isAttachedToWindow) { | |
// We're already attached, just request as normal | |
requestApplyInsets() | |
} else { | |
// We're not attached to the hierarchy, add a listener to | |
// request when we are | |
addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener { | |
override fun onViewAttachedToWindow(v: View) { | |
v.removeOnAttachStateChangeListener(this) | |
v.requestApplyInsets() | |
} | |
override fun onViewDetachedFromWindow(v: View) = Unit | |
}) | |
} | |
} | |
fun View.doOnApplyWindowInsets(f: (View, WindowInsets, InitialPadding) -> Unit) { | |
// Create a snapshot of the view's padding state | |
val initialPadding = recordInitialPaddingForView(this) | |
// Set an actual OnApplyWindowInsetsListener which proxies to the given | |
// lambda, also passing in the original padding state | |
setOnApplyWindowInsetsListener { v, insets -> | |
f(v, insets, initialPadding) | |
// Always return the insets, so that children can also use them | |
insets | |
} | |
// request some insets | |
requestApplyInsetsWhenAttached() | |
} | |
data class InitialPadding( | |
val left: Int, val top: Int, | |
val right: Int, val bottom: Int | |
) | |
private fun recordInitialPaddingForView(view: View) = InitialPadding( | |
view.paddingLeft, view.paddingTop, view.paddingRight, view.paddingBottom | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment