Assume that we want all toasts to be LENGTH_SHORT
. Two toast extensions functions of Context
are enough.
fun Context.makeToast(message: String)
= Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
fun Context.makeToast(@StringRes message: Int)
= Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
A Context
is related to UIs. Toast extension functions are extension functions of Context
only. Extension functions of String
are related to operations on strings. Extension functions of Int
related to operations on integers. Strings and integers do not operate on toasts.
Extension functions are organized around the classes they extend. So, a file named ContextExt.kt
would define the two makeToast
extension functions illustrated above.