Skip to content

Instantly share code, notes, and snippets.

@adib2149
Forked from adavis/CommonExtensions.kt
Created June 8, 2017 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adib2149/e45ed4ca3dab9dcdba3821746f671236 to your computer and use it in GitHub Desktop.
Save adib2149/e45ed4ca3dab9dcdba3821746f671236 to your computer and use it in GitHub Desktop.
Common Android Extensions in Kotlin
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment