How to Create Custom Toast in Android
Make your Toast more attractive
You can implement custom toast in two ways:
Method 1 : create your own toast layout without external resources
Method 2 : You can use external libraries to make custom toast
First will implement Custom Toast using method 1
Steps:
Step 1: create drawable file for background
Step 2: create custom layout
Step 3: inflate the layout into view and set into toast object
Next will implement Custom Toast using method 2
There are many external libraries to implement custom toast
but in this post we will use Toasty library. In this library there are 5 types of custom toast:
success , error , info , warning and normal
Steps:
Step 1: Add this in your root build.gradle file (not your module build.gradle file):
allprojects {
repositories {
…
maven { url “https://jitpack.io" }
}
}
Step 2: Add this to your module’s build.gradle file (make sure the version matches the JitPack badge above):
dependencies {
...
implementation 'com.github.GrenderG:Toasty:1.5.0'
}
Step 3: Go to java file and show the toast as below
Toasty.success(getApplicationContext(),"This is success toast",Toast.LENGTH_LONG).show();// or Toasty.error(getApplicationContext(),"This is error toast",Toast.LENGTH_LONG).show();
Result:
Source code: