Skip to content

Instantly share code, notes, and snippets.

@bluemix
Forked from adavis/CommonExtensions.kt
Last active June 6, 2017 22:16
Show Gist options
  • Save bluemix/23e5e4f1d49a77be4b601b1937cbe74f to your computer and use it in GitHub Desktop.
Save bluemix/23e5e4f1d49a77be4b601b1937cbe74f 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)
fun Activity.showOnUiThread(init: Activity.() -> Unit): Activity {
if (!isFinishing) {
runOnUiThread {
init()
}
}
return this
}
fun Activity.showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment