Created
December 8, 2025 18:36
-
-
Save mutkuensert/d45a9ab32704a5a971d19c03def00768 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 android.view.View | |
| import android.view.ViewGroup | |
| import androidx.core.view.ViewCompat | |
| import androidx.core.view.WindowInsetsCompat | |
| import androidx.core.view.updateMargins | |
| import androidx.core.view.updatePadding | |
| import com.example.R | |
| fun View.addStatusBarPadding() { | |
| ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets -> | |
| if (getTag(R.id.appliedStatusBarPadding) == null) { | |
| setTag(R.id.appliedStatusBarPadding, true) | |
| val statusBarHeight = insets.getInsets(WindowInsetsCompat.Type.systemBars()).top | |
| this.updatePadding(top = paddingTop + statusBarHeight) | |
| layoutParams.height = layoutParams.height + statusBarHeight | |
| } | |
| WindowInsetsCompat.CONSUMED | |
| } | |
| } | |
| fun View.addStatusBarMargin() { | |
| ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets -> | |
| if (getTag(R.id.appliedStatusBarMargin) == null) { | |
| setTag(R.id.appliedStatusBarMargin, true) | |
| val statusBarHeight = | |
| insets.getInsets(WindowInsetsCompat.Type.systemBars()).top | |
| val params = layoutParams as ViewGroup.MarginLayoutParams | |
| params.updateMargins(top = params.topMargin + statusBarHeight) | |
| layoutParams = params | |
| } | |
| WindowInsetsCompat.CONSUMED | |
| } | |
| } | |
| fun View.addNavigationBarPadding() { | |
| ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets -> | |
| if (getTag(R.id.appliedNavigationBarPadding) == null) { | |
| setTag(R.id.appliedNavigationBarPadding, true) | |
| val navigationBarHeight = | |
| insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom | |
| updatePadding(bottom = paddingBottom + navigationBarHeight) | |
| layoutParams.height = layoutParams.height + navigationBarHeight | |
| } | |
| WindowInsetsCompat.CONSUMED | |
| } | |
| } | |
| fun View.addNavigationBarMargin() { | |
| ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets -> | |
| if (getTag(R.id.appliedNavigationBarMargin) == null) { | |
| setTag(R.id.appliedNavigationBarMargin, true) | |
| val navigationBarHeight = | |
| insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom | |
| val params = layoutParams as ViewGroup.MarginLayoutParams | |
| params.updateMargins(bottom = params.bottomMargin + navigationBarHeight) | |
| layoutParams = params | |
| } | |
| WindowInsetsCompat.CONSUMED | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment