Last active
April 2, 2024 20:51
-
-
Save adavis/8d666aa48dba524415b707296b1329ed to your computer and use it in GitHub Desktop.
Common Android Extensions in Kotlin
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.visible() { | |
visibility = View.VISIBLE | |
} | |
fun View.invisible() { | |
visibility = View.INVISIBLE | |
} | |
fun View.gone() { | |
visibility = View.GONE | |
} | |
fun Context.inflate(res: Int, parent: ViewGroup? = null) : View { | |
return LayoutInflater.from(this).inflate(res, parent, false) | |
} | |
inline fun Dialog.ifIsShowing(body: Dialog.() -> Unit) { | |
if (isShowing) { | |
body() | |
} | |
} | |
inline fun Snackbar.ifIsShowing(body: Snackbar.() -> Unit) { | |
if (isShown) { | |
body() | |
} | |
} | |
inline fun ViewGroup.forEach(action: (View) -> Unit) { | |
for (index in 0 until childCount) { | |
action(getChildAtIndex(index)) | |
} | |
} | |
operator fun ViewGroup.get(position: Int): View? = getChildAt(position) |
mmlovesyy
commented
Nov 18, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment