Android — Display Custom Toast Messages
Hey. In this article we are gonna see who can we create and display custom Toast messages in Android application.
Andorid Toast can be used to display information for the short period of time. A toast contains message to be displayed quickly and disappears after sometime.
For this tutorial we are gonna use TanTinToast library you can find here.
Prerequisites
Add this in your root build.gradle
file (not your module build.gradle
file):
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Dependency
Add this to your module’s build.gradle
file (make sure the version matches the JitPack badge above):
dependencies {
...
implementation 'com.github.Evangelidis91:TanTinToast:1.1.3'
}
Usage
DON’T FORGET THE show()
METHOD!
Using this library you can display very easy some already implemented Toast messages or create your own.
You can display:
- Error Toast
- Success Toast
- Info Toast
- Warning Toast
- Normal Toast
- Custom Toast
Using Kotlin
To display an error Toast:TanTinToast.Error(this).text("Error Toast").show()
To display Success Toast:TanTinToast.Success(this).text("Success Toast").show()
To display Info Toast:TanTinToast.Info(this).text("Info Toast").show()
To display Warning Toast:TanTinToast.Warning(this).text("Warning Toast").show()
To display Normal Toast:TanTinToast.Normal(this).text("Normal Toast").show()
To display Custom Toast:
TanTinToast.Custom(this)
.text("Custom Toast")
.icon(R.drawable.icon)
.background(R.color.colorPrimaryDark)
.textColor(R.color.colorAccent)
.time(TanTinToast.LONG)
.typeface(R.font.montserrat)
.show()
Using Java
To display an error Toast:new TanTinToast.Error(this).text("Error Toast ").show();
To display Success Toast:new TanTinToast.Success(this).text("Success Toast").show();
To display Info Toast:new TanTinToast.Info(this).text("Info Toast").show();
To display Warning Toast:new TanTinToast.Warning(this).text("Warning Toast").show();
To display Normal Toast:new TanTinToast.Normal(this).text("Normal Toast").show();
To display Custom Toast:new TanTinToast.Custom(this).text("Custom Toast").show();
Customize Toast text
.text("Any text")
Customize Toast time
.time(TanTinToast.LONG)
Use TanTinToast.LONG
to display toast for 3.5 seconds or TanTinToast.SHORT
to display for 2 seconds.
Customize Toast message FontFamily
.typeface(R.font.custom_font_family)
You can just pass any file under font folder
Customize Toast message TextSIze
.textSize(16)
Use this method to change Toast message text size. Inserted value declares sp value
Using TanTin.Custom
Using Custom you can customize
- The icon.
- The backgroundColor.
- The textColor.
- All the previous customizations.
Use .icon(int)
to set Toast icon.
Use .background(int)
to set Toast backgroundColor.
Use .textColor(int)
to set Toast message textColor.
I hope you found this tutorial and this library really helpfull.
Source code here
Feel free to leave any comment.
Thank you very much