Skip to content

Instantly share code, notes, and snippets.

@baeyeongpyo
Forked from HarryTylenol/AnkoExtensions.kt
Created June 19, 2018 00:29
Show Gist options
  • Select an option

  • Save baeyeongpyo/4fc23bf9c8d4a8e4c2ba78a6fdb5c043 to your computer and use it in GitHub Desktop.

Select an option

Save baeyeongpyo/4fc23bf9c8d4a8e4c2ba78a6fdb5c043 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