Skip to content

Instantly share code, notes, and snippets.

@ch8n
Last active February 25, 2020 12:01
Show Gist options
  • Save ch8n/1a0a36618e6948f3ac9ce5d4b80c0588 to your computer and use it in GitHub Desktop.
Save ch8n/1a0a36618e6948f3ac9ce5d4b80c0588 to your computer and use it in GitHub Desktop.
Some Cool Extensions!
// View Visisibility toggle
fun View.setVisibility(isVisibe: Boolean) = if (isVisibe) {
this.setVisibility(View.VISIBLE)
} else {
this.setVisibility(View.GONE)
}
//concise start activity
inline fun <reified T : AppCompatActivity> AppCompatActivity.launchActivity(bundle: Bundle = android.os.Bundle()) {
startActivity(android.content.Intent(this as Context, T::class.java).also {
it.putExtras(bundle)
})
}
// try or null
fun <T> tryOrNull(expression: () -> T): T? = try {
expression.invoke()
} catch (e: Exception) {
null
}
//viewgroup to inflate for recyclerviews
inline fun ViewGroup.inflate(@LayoutRes resourceId: Int): View? {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as? LayoutInflater
return inflater?.inflate(resourceId, null,false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment