Skip to content

Instantly share code, notes, and snippets.

@jimmyFlash
Last active December 27, 2019 15:42
Show Gist options
  • Save jimmyFlash/b2fd7e7b17f63ef814c9f4dd435390a9 to your computer and use it in GitHub Desktop.
Save jimmyFlash/b2fd7e7b17f63ef814c9f4dd435390a9 to your computer and use it in GitHub Desktop.
Set of extension function to use in your activity to display Alertdilog, Snackbar, toast or navigate to new activity
fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) {
val snack = Snackbar.make(this, message, length)
snack.f()
snack.show()
}
fun Snackbar.action(action: String, color: Int? = null, listener: (View) -> Unit) {
setAction(action, listener)
color?.let { setActionTextColor(color) }
}
fun Context.showToast( message: String, length: Int = Toast.LENGTH_SHORT){
Toast.makeText(this, message, length).show()
}
fun @receiver:StringRes Int.errorDialog(activity: Activity) {
AlertDialog.Builder(activity, android.R.style.Theme_Material_Dialog_Alert)
.setTitle("Error")
.setMessage(this@errorDialog)
.setPositiveButton(android.R.string.ok) { dialog, _ -> dialog.dismiss() }
.setIcon(android.R.drawable.ic_dialog_alert).show()
}
fun <T : Activity> KClass<T>.start(activity: Activity, extras : Bundle = Bundle(), finish: Boolean = false) {
Intent(activity, this.java).apply {
putExtras(extras)
activity.startActivity(this)
}
if (finish) {
activity.finish()
}
}
fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View {
return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment