Last active
February 25, 2020 12:01
-
-
Save ch8n/1a0a36618e6948f3ac9ce5d4b80c0588 to your computer and use it in GitHub Desktop.
Some Cool Extensions!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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