Last active
September 13, 2023 05:45
-
-
Save kibotu/60ab145cff5cd0aa95557217d8940c99 to your computer and use it in GitHub Desktop.
Kotlin extension method to set margin.
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.setMargins( | |
left: Int? = null, | |
top: Int? = null, | |
right: Int? = null, | |
bottom: Int? = null | |
) { | |
val lp = layoutParams as? ViewGroup.MarginLayoutParams | |
?: return | |
lp.setMargins( | |
left ?: lp.leftMargin, | |
top ?: lp.topMargin, | |
right ?: lp.rightMargin, | |
bottom ?: lp.bottomMargin | |
) | |
layoutParams = lp | |
} |
Thanks!😁
bottom ?: lp.bottomMargin
right oops my bad :D
Thank you for your code
Thanks. Need to add start margin and end margin.
fun View.setMargins(
left: Int? = null,
top: Int? = null,
right: Int? = null,
bottom: Int? = null
) {
val lp = layoutParams as? ViewGroup.MarginLayoutParams
?: return
lp.setMargins(
left ?: lp.leftMargin,
top ?: lp.topMargin,
right ?: lp.rightMargin,
bottom ?: lp.bottomMargin
)
lp.marginStart = lp.leftMargin
lp.marginEnd = lp.rightMargin
layoutParams = lp
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works really well. Thanks.