Wednesday, 2 March 2022

Show Toast in Android Studio Java Example

Toast is one of the basic functionality of app that you can use to notify a user in a simple text. Toast in android device looks something like below, but its design varies from device to device.
Android Toast Example in Java

EXAMPLE 1

Below is how you can show a basic Toast.

Toast toast = Toast.makeText(getApplicationContext(), "This is a Message to user", Toast.LENGTH_LONG); // initiate the Toast with context, message and duration
toast.show(); // display the Toast

The 3rd Parameter is for duration in abouve example. You can use Toast.LENGTH_LONG or Toast.LENGTH_SHORT for long or short duration.

EXAMPLE 2

You can also use setGravity() to set Position for the Toast.
Toast toast = Toast.makeText(getApplicationContext(), "Simple Toast In Android", Toast.LENGTH_LONG); // initiate the Toast with context, message and duration
toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);// You can set gravity for the Toast.
toast.show(); // display the Toast

You can use Libraries such as FancyToast to show some classic Toasts in Android.

FancyToast Github Example Android Java Kotlin

0 Please Share a Your Opinion.:

Comment something useful and creative :)