Skip to content

Instantly share code, notes, and snippets.

@HarryTylenol
Created June 2, 2018 03:37
Show Gist options
  • Save HarryTylenol/8d94533b2696f30634cdf4d08fdf3b34 to your computer and use it in GitHub Desktop.
Save HarryTylenol/8d94533b2696f30634cdf4d08fdf3b34 to your computer and use it in GitHub Desktop.
Anko Stream Extensions
inline fun <V : View> V.alpha(alpha: Double): V {
this.alpha = alpha.toFloat()
return this
}
inline fun <V : View> V.backgroundColor(color: Int): V {
backgroundColor = color
return this
}
inline fun <V : View> V.backgroundRes(res: Int): V {
backgroundResource = res
return this
}
inline fun <V : View> V.background(drawable: Drawable): V {
background = drawable
return this
}
inline fun <V : View> V.focus(crossinline callback: V.(Boolean) -> Unit): V {
setOnFocusChangeListener { view, b -> callback(b) }
return this
}
inline fun <V : View> V.click(crossinline callback: V.() -> Unit): V {
setOnClickListener { callback() }
return this
}
inline fun <V : TextView> V.textRes(@StringRes text: Int): V {
textResource = text
return this
}
inline fun <V : TextView> V.hintRes(@StringRes text: Int): V {
hintResource = text
return this
}
inline fun <V : TextView> V.hint(text: String): V {
this.hint = text
return this
}
inline fun <V : TextView> V.text(text: String): V {
this.text = text
return this
}
inline fun <V : TextView> V.size(size: Int): V {
textSize = size.toFloat()
return this
}
inline fun <V : TextView> V.colorRes(@ColorRes color: Int): V {
textColorResource = color
return this
}
inline fun <V : TextView> V.color(@ColorInt color: Int): V {
textColorResource = color
return this
}
inline fun <T : TextView> T.font(@FontRes res: Int = R.font.bm_dohyeon): T {
typeface = ResourcesCompat.getFont(context, res)
return this
}
inline fun MaterialButton.radius(radius: Int): MaterialButton {
cornerRadius = radius
return this
}
inline fun MaterialButton.backgroundTintRes(@ColorRes color: Int): MaterialButton {
backgroundTintList = ColorStateList.valueOf(context.color(color))
return this
}
inline fun MaterialButton.backgroundTint(@ColorInt color: Int): MaterialButton {
backgroundTintList = ColorStateList.valueOf(color)
return this
}
verticalLayout {
padding = dip(24)
textView().size(14).lparams { rightMargin = dip(4) }
button()
.size(14)
.colorRes(R.color.colorAccent)
.click {
// Click Action
}
}.backgroundColor(Color.WHITE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment