Skip to content

Instantly share code, notes, and snippets.

@underwindfall
Created April 11, 2019 09:17
Show Gist options
  • Save underwindfall/b734072c9c921d840f6bc57360bdc9d9 to your computer and use it in GitHub Desktop.
Save underwindfall/b734072c9c921d840f6bc57360bdc9d9 to your computer and use it in GitHub Desktop.
/**
* Utility functions for Android View
*/
fun View.setZTranslation(z: Float) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
this.translationZ = z
}
}
/**
* Get a typedArray to obtain styledAttributes and recycle it
*/
inline fun View.withStyleableRes(set: AttributeSet, @StyleableRes attrs: IntArray, init: TypedArray.() -> Unit) {
val typedArray = context.theme.obtainStyledAttributes(set, attrs, 0, 0)
if ( typedArray != null ) {
try {
typedArray.init()
} finally {
typedArray.recycle()
}
}
}
/**
* Layout inflation shorter form
*/
fun ViewGroup.inflateLayout(layoutId: Int, attachToRoot: Boolean = true) {
android.view.LayoutInflater.from(context).inflate(layoutId, this, attachToRoot)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment