Material Toast let you customize Android Toasts easily: colors, shapes, elevations, icons, etc.
This library can be found in jCenter repository
implementation 'com.javiersc:materialtoast:1.0.2'
implementation("com.javiersc:materialtoast:1.0.2")
Normal | Success |
---|---|
Warning | Error |
---|---|
MaterialToast.normal(context, "Your text").show()
// You can use the Context extension function too
normal("Your text").show()
// Or if <this> is not the context
context.normal("Your text").show()
Similarly, you can use the other states with warning(...)
, error(...)
, etc.
For status toasts you can customize only the icon and duration.
They include default icons and a Toast.LENGTH_LONG
duration.
Example 1 | Example 2 |
---|---|
Example 3 |
---|
Create a Stylized
and pass it as parameter of the stylized function.
Example 2 stylized:
val myStylized = Stylized {
message = textStylized
messageColor = ContextCompat.getColor(this@MainActivity, R.color.stylizedText2)
icon = ContextCompat.getDrawable(this@MainActivity, R.drawable.ic_stylized_2)
iconTint = ContextCompat.getColor(this@MainActivity, R.color.stylizedIcon2)
backgroundColor = ContextCompat.getColor(this@MainActivity, R.color.stylizedBackground2)
strokeWidth = 2.dp
strokeColor = ContextCompat.getColor(this@MainActivity, R.color.stylizedStroke2)
shape = ToastShape.CUT
radius = 8f.dp
}
MaterialToast.stylized(myStylized).show()
// You can use the Context extension function too
stylized(myStylized).show()
// Or if <this> is not the context
context.stylized(myStylized).show()
Property | Type | Default value | Values |
---|---|---|---|
message | String | "" | |
messageColor | Int | "#000000".toColorInt() | |
messageSize | Float | 14f.sp | |
typeface | Typeface | Typeface.DEFAULT | |
icon | Drawable? | null | |
iconTint | Int | "#000000".toColorInt() | |
backgroundColor | Int | "#DFFFFFFF".toColorInt() | |
elevation | Float | 0f | |
strokeColor | Int | "#DFFFFFFF".toColorInt() | |
strokeWidth | Int | 0 | |
radius | Float | 16f.dp | |
offsetY | Float | 6f.dp | |
shape | ToastShape | ToastShape.ROUNDED | ROUNDED and CUT |
duration | Int | Toast.LENGTH_LONG | LENGTH_LONG and LENGTH_SHORT |
Check app module to see the code or compile this project to see in action
To change the dimensional values programmatically, you can use some extensions included in this library. These extensions convert these values to pixels in an elegant way.
For example, for a Float
value equal to 4 dp
val myRadius: Float = 4f.dp
val myTextSize: Float = 4f.sp
Similarly, for Int
values
val myNumber: Int = 4.dp