Motion Toast in Android
In this blog, we’re going to discuss motion toast in android. Before going to implement the motion toast in android, first, let’s see what motion toast in android is.
If you are not familiar with what is a Toast in android, then go through my previous blog what is Toast in Android?
What is Motion Toast?
Motion Toast is a beautiful toast library for android. Generally, it is for Kotlin. But here I implement it for java. Motion toast makes a toast message impressive and beautiful.
Dependency Project Level
Step 1. Add the JitPack repository to your build file
Add it to your root build.gradle at the end of repositories:
allprojects {
repositories {
…
maven { url ‘https://jitpack.io' }
}
}
Dependency App Level
Add a dependency in your app module
dependencies {
implementation ‘com.github.Spikeysanju:MotionToast:1.3.3.1
}
Types of Toast style
Mainly four types of toast style, those are:
- Motion Toast
- Color Motion Toast
- Dark Toast
- Dark Color Toast
Toast Types
There are five motion toast types. Those are:
TOAST_SUCCESS
TOAST_ERROR
TOAST_WARNING
TOAST_INFO
TOAST_DELETE
Toast Durations
There are two types of durations in the motion library. Those are:
LONG_DURATION : The toast message is stay for 4 seconds only
SHORT_DURATION : The toast message is stay for 2 seconds only
Usage
Sample Code for — Motion Toast
Success Toast
MotionToast.createToast(this,"Upload Completed!",MotionToast.TOAST_SUCCESS,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Error Toast
MotionToast.createToast(this,"Profile Update Failed!",MotionToast.TOAST_ERROR,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Warning Toast
MotionToast.createToast(this,"Please fill all the details!",MotionToast.TOAST_WARNING,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Info Toast
MotionToast.createToast(this,"This is information toast!",MotionToast.TOAST_INFO,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Sample Code for — Color Motion Toast
Success Toast
MotionToast.createColorToast(this,"Upload Completed!",MotionToast.TOAST_SUCCESS,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Error Toast
MotionToast.createColorToast(this,"Profile Update Failed!",MotionToast.TOAST_ERROR,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Warning Toast
MotionToast.createColorToast(this,"Please fill all the details!",MotionToast.TOAST_WARNING,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Info Toast
MotionToast.createColorToast(this,"This is information toast!",MotionToast.TOAST_INFO,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Sample Code for — Dark Toast
Success Toast
MotionToast.darkToast(this,"Upload Completed!",MotionToast.TOAST_SUCCESS,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Error Toast
MotionToast.darkToast(this,"Profile Update Failed!",MotionToast.TOAST_ERROR,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Warning Toast
MotionToast.darkToast(this,"Please fill all the details!",MotionToast.TOAST_WARNING,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Info Toast
MotionToast.darkToast(this,"This is information toast!",MotionToast.TOAST_INFO,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Sample Code for — Dark Color Toast
Success Toast
MotionToast.darkColorToast(this,"Upload Completed!",MotionToast.TOAST_SUCCESS,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Error Toast
MotionToast.darkColorToast(this,"Profile Update Failed!",MotionToast.TOAST_ERROR,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Warning Toast
MotionToast.darkColorToast(this,"Please fill all the details!",MotionToast.TOAST_WARNING,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Info Toast
MotionToast.darkColorToast(this,"This is information toast!",MotionToast.TOAST_INFO,MotionToast.GRAVITY_BOTTOM,MotionToast.LONG_DURATION,ResourcesCompat.getFont(this,R.font.helvetica_regular))
Example of Motion Toast
activity_toast.xml
<?xml version=”1.0" encoding=”utf-8"?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:app=”http://schemas.android.com/apk/res-auto"
xmlns:tools=”http://schemas.android.com/tools"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center_horizontal”
android:orientation=”vertical”
android:padding=”20dp”
tools:context=”.ToastActivtiy”>
<Button
android:id=”@+id/successToast”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/success_toast” />
<Button
android:id=”@+id/errorToast”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/error_toast” />
<Button
android:id=”@+id/warningToast”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/warning_toast” />
<Button
android:id=”@+id/infoToast”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/info_toast” />
</LinearLayout>
ToastActivty.java
package com.codewithgolap.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import www.sanju.motiontoast.MotionToast;
public class ToastActivtiy extends AppCompatActivity {
Button successToast, errorToast, warningToast, infoToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast_activtiy);
successToast = findViewById(R.id.successToast);
errorToast = findViewById(R.id.errorToast);
warningToast = findViewById(R.id.warningToast);
infoToast = findViewById(R.id.infoToast);
successToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MotionToast.Companion.createToast(ToastActivtiy.this,”Success Toast”,
MotionToast.TOAST_SUCCESS,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(ToastActivtiy.this, R.font.helvetica_regular));
}
});
errorToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MotionToast.Companion.createColorToast(ToastActivtiy.this,”Error Toast”,
MotionToast.TOAST_ERROR,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(ToastActivtiy.this, R.font.helvetica_regular));
}
});
warningToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MotionToast.Companion.darkToast(ToastActivtiy.this,”Warning Toast”,
MotionToast.TOAST_WARNING,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(ToastActivtiy.this, R.font.helvetica_regular));
}
});
infoToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MotionToast.Companion.darkColorToast(ToastActivtiy.this,”Info Toast”,
MotionToast.TOAST_INFO,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(ToastActivtiy.this, R.font.helvetica_regular));
}
});
}}
Output
Find it helpful? Let me know and show some love by following me and visit my website www.gbandroidblogs.com to know more about android related topics.