Android library to create customizable floating animated toasts like in Clash Royale app

Last update: Mar 9, 2022

FloatingToast-Android

An android library to make customisable floating animated toasts

Android Arsenal

Screenshots

Getting Started

In your build.gradle

dependencies {
    implementation 'hari.floatingtoast:floatingtoast:0.1.1'
}

Usage

Create Floating Toasts

First, instantiate a FloatingToast object with one of the makeToast() methods. This method takes three parameters: the view which calls the toast (recommended) or the activity context, the text message, and the duration for the toast. It returns a properly initialized FloatingToast object. You can display the toast notification with show(), as shown in the following example:

    Button button = findViewById(R.id.button);
    String text = "Hello toast!";
    int duration = FloatingToast.LENGTH_MEDIUM;

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            FloatingToast toast = FloatingToast.makeToast(button, text, FloatingToast.LENGTH_MEDIUM);
            toast.show();
        }
    });

You can also chain your methods and avoid holding on to the Toast object, like this:

    FloatingToast.makeToast(button, text, FloatingToast.LENGTH_MEDIUM).show();

You can use the activity context to instantiate the FloatingToast object, like this:

    FloatingToast.makeToast(MainActivity.this, text, FloatingToast.LENGTH_MEDIUM).show();
    NOTE: Always use the view (which was used to call the toast) to
    create the FloatingToast object wherever possible rather than
    using the activity context.

An example with all the customisations:

    Typeface customFont = Typeface.createFromAsset(getContext().getAssets(), "fonts/custom_font.ttf");

    //Duration of 1250 millis
    //Gravity - Mid Top                 (Default is Center)
    //Fade out duration of 1000 millis  (Default is 750 millis)
    //Text size - 12dp                  (Default is 16dp)
    //Background blur - On              (Default is On)
    //Float distance - 30px             (Default is 40px)
    //Text color - White
    //Text shadow - On                  (Default is Off)
    //Custom font                       (No custom font is provided by default)
    FloatingToast.makeToast(button, text, FloatingToast.LENGTH_LONG)
            .setGravity(FloatingToast.GRAVITY_MID_TOP)
            .setFadeOutDuration(FloatingToast.FADE_DURATION_LONG)
            .setTextSizeInDp(12)
            .setBackgroundBlur(true)
            .setFloatDistance(FloatingToast.DISTANCE_SHORT)
            .setTextColor(Color.parseColor("#ffffff"))
            .setShadowLayer(5, 1, 1, Color.parseColor("#000000"))
            .setTextTypeface(customFont)
            .show();    //Show toast at the specified fixed position

You can also set the toast's position as dynamic(i.e. show the toast at the touch position) like this:

    FloatingToast.makeToast(button, text, FloatingToast.LENGTH_MEDIUM)
            .showAtTouchPosition();

Summary

Constants

Duration - Duration of the toast being shown. This time could be user-definable.
int LENGTH_LONG

Show the toast for a long period of time. (1250 millis)

int LENGTH_MEDIUM

Show the toast for a certain period of time. (1000 millis)

int LENGTH_SHORT

Show the toast for a short period of time. (750 millis)

Fade out duration - Fade out duration of the toast. This time could be user-definable.
See also setFadeOutDuration(int)
int FADE_DURATION_LONG

Fade the toast within a long period of time. (1000 millis)

int FADE_DURATION_MEDIUM

Fade the toast within a certain period of time. (750 millis)

int FADE_DURATION_SHORT

Fade the toast within a short period of time. (500 millis)

Float distance - Float distance of the toast. This distance could be user-definable.
See also setFloatDistance(float)
float DISTANCE_LONG

Float the toast for a long distance. (50 px)

float DISTANCE_MEDIUM

Float the toast for a certain distance. (40 px)

float DISTANCE_SHORT

Float the toast for a short distance. (30 px)

Gravity - Location at which the toast should appear on the screen. This could be user-definable.
See also setGravity(int)
int GRAVITY_MID_TOP

Show the toast at 25% from the top of the screen.

int GRAVITY_CENTER

Show the toast at the center of the screen.

int GRAVITY_MID_BOTTOM

Show the toast at 75% from the top of the screen.

int GRAVITY_TOP

Show the toast at the top of the screen.

int GRAVITY_BOTTOM

Show the toast at the bottom of the screen.

Style - Style of the text in the toast. This style could be user-definable.
See also setTextStyle(int)
int STYLE_BOLD

Bold style for the text in the toast.

int STYLE_ITALIC

Italic style for the text in the toast.

int STYLE_NORMAL

Normal style for the text in the toast.

int STYLE_BOLD_ITALIC

Bold Italic style for the text in the toast.


Public methods

static FloatingToast makeToast(View view, String text, int duration)

Make a standard toast that just contains a text view.

static FloatingToast makeToast(View view, int resId, int duration)

Make a standard toast that just contains a text view with the text from a resource.

static FloatingToast makeToast(Activity activity, String text, int duration)

Make a standard toast that just contains a text view.

static FloatingToast makeToast(Activity activity, int resId, int duration)

Make a standard toast that just contains a text view with the text from a resource.

FloatingToast setGravity(int gravity)

Set the location at which the notification should appear on the screen.

FloatingToast setGravity(int gravity, int xOffset, int yOffset)

Set the location at which the notification should appear on the screen.

FloatingToast setFadeOutDuration(int fadeOutDuration)

Set the fade out duration of the toast.

FloatingToast setTextColor(int color)

Sets the text color for all the states (normal, selected, focused) to be this color.

FloatingToast setTextTypeface(Typeface typeface)

Sets the typeface and style in which the text should be displayed.

FloatingToast setTextStyle(int style)

Sets the style in which the text should be displayed, and turns on the fake bold and italic bits in the Paint if the Typeface that you provided does not have all the bits in the style that you specified.

FloatingToast setTextSizeInSp(float sizeInSp)

Set the default text size to the given value, interpreted as "scaled pixel" units. This size is adjusted based on the current density and user font size preference.

FloatingToast setTextSizeInDp(float sizeInDp)

Set the default text size to the given value, interpreted as "density independent pixel" units. This size is adjusted based on the current density.

FloatingToast setTextSizeCustomUnit(int unit, float size)

Set the default text size to a given unit and value.

FloatingToast setFloatDistance(float floatDistance)

Sets the distance of the toast till which it floats.

FloatingToast setShadowLayer(float shadowRadius, float shadowDx, float shadowDy, int shadowColor)

Gives the text a shadow of the specified blur radius and color, the specified distance from its drawn position.

void setBackgroundBlur(boolean bool)

Sets the blur background for the text in the toast.

void show()

Show the view for the specified duration.

Public methods

makeToast

    makeToast (View view, String text, int duration)

Make a standard toast that just contains a text view.
(Recommended method over makeToast(Activity, String, int)

Parameters
view View: The view which was used to call the toast.
text String: The text to show. Can be formatted text.
duration int: Duration of the toast to be shown in milliseconds. Either user-defined int or predefined constant duration.

makeToast

    makeToast (View view, int resId, int duration)

Make a standard toast that just contains a text view with the text from a resource.
@throws Resources.NotFoundException if the resource can't be found.
(Recommended method over makeToast(Activity, int, int)

Parameters
view View: The view which was used to call the toast.
resId int: The resource id of the string resource to use. Can be formatted text.
duration int: Duration of the toast to be shown in milliseconds. Either user-defined int or predefined constant duration.

makeToast

    makeToast (Activity activity, String text, int duration)

Make a standard toast that just contains a text view.
(Only use this method if not calling from a view)

Parameters
actvity Activity: The activity context. Usually your android.app.Activity object.
text String: The text to show. Can be formatted text.
duration int: Duration of the toast to be shown in milliseconds. Either user-defined int or predefined constant duration.

makeToast

    makeToast (Activity activity, int resId, int duration)

Make a standard toast that just contains a text view with the text from a resource.
@throws Resources.NotFoundException if the resource can't be found.
(Only use this method if not calling from a view)

Parameters
actvity Activity: The activity context. Usually your android.app.Activity object.
resId int: The resource id of the string resource to use. Can be formatted text.
duration int: Duration of the toast to be shown in milliseconds. Either user-defined int or predefined constant duration.

setGravity

    setGravity (int gravity)

Set the location at which the notification should appear on the screen.

Parameters
gravity int: Position of toast in the screen.

setGravity

    setGravity (int gravity, int xOffset, int yOffset)

Set the location at which the notification should appear on the screen.

Parameters
gravity int: Position of toast in the screen.
xOffset int: X offset in pixels to apply to the gravity's location.
yOffset int: Y offset in pixels to apply to the gravity's location

setFadeOutDuration

    setFadeOutDuration (int fadeOutDuration)

Set the fade out duration of the toast. Total animation time of the toast - duration + fadeOutDuration

Parameters
fadeOutDuration int: Fade out duration in milliseconds

setTextColor

    setTextColor (int color)

Sets the text color for all the states (normal, selected, focused) to be this color.

Parameters
color int: A color value in the form 0xAARRGGBB.
Do not pass a resource ID. To get a color value from a resource ID, call android.support.v4.content.ContextCompat#getColor(Context, int) getColor

setTextTypeface

    setTextTypeface (Typeface typeface)

Sets the typeface and style in which the text should be displayed. Note that not all Typeface families actually have bold and italic variants, so you may need to use setTextStyle(int) to get the appearance that you actually want.

Parameters
typeface Typeface

setTextStyle

    setTextStyle (int style)

Sets the style in which the text should be displayed, and turns on the fake bold and italic bits in the Paint if the Typeface that you provided does not have all the bits in the style that you specified.

Parameters
style int

setTextSizeInSp

    setTextSizeInSp (float sizeInSp)

Set the default text size to the given value, interpreted as "scaled pixel" units. This size is adjusted based on the current density and user font size preference.

Parameters
sizeInSp float: The scaled pixel size.

setTextSizeInDp

    setTextSizeInDp (int sizeInDp)

Set the default text size to the given value, interpreted as "independent pixel" units. This size is adjusted based on the current density.

Parameters
sizeInDp int: The density independent pixel size.

setTextSizeCustomUnit

    setTextSizeCustomUnit (int unit, float size)

Set the default text size to a given unit and value. See android.util.TypedValue for the possible dimension units.

Parameters
unit int: The desired dimension unit.
size float: The desired size in the given units.

setFloatDistance

    setFloatDistance (float floatDistance)

Sets the distance of the toast till which it floats.

Parameters
floatDistance float: Distance in pixels

setShadowLayer

    setShadowLayer (float shadowRadius, float shadowDx, float shadowDy, int shadowColor)

Gives the text a shadow of the specified blur radius and color, the specified distance from its drawn position. The text shadow produced does not interact with the properties on view that are responsible for real time shadows, elevation and translationZ.

Parameters
shadowRadius float
shadowDx float
shadowDy float
shadowColor int

setBackgroundBlur

    setBackgroundBlur (boolean bool)

Sets the blur background for the text in the toast.

Parameters
bool boolean: false disables the blur. Default is true.

showAtTouchPosition

    showAtTouchPosition (View view)

Show the view for the specified duration at the touch position.

Parameters
view View: View over which the toast is to be shown

show

    show()

Show the view for the specified duration.


Show your support

Give a if this project helped you!

License

Copyright ©️ 2018 Hariprasanth S

This project is licensed under the Apache License, Version 2.0
You may also obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

GitHub

https://github.com/hariprasanths/FloatingToast
You might also like...

A really simple library that help you to display a custom toast with many colors (for : success, warning, danger, info, dark, light, primary...etc ), or with rounded corners, or event with image.

CoolToast A really simple library that help you to display a custom toast with many colors (for : success, warning, danger, info, dark, light, primary

Jun 22, 2020

Custom Toast Library by Google Developer Student Club University of Brawijaya

Custom Toast Library by Google Developer Student Club University of Brawijaya

GDSCToast Custom Toast Library by Google Developer Student Club University of Brawijaya Prerequisites For old version of gradle (before arctic fox upd

May 22, 2022

MDToast - MaterialDesign Toast library written with Kotlin with lots of extensions to interact easily in contexts

MDToast - MaterialDesign Toast library written with Kotlin with lots of extensions to interact easily in contexts

MDToast - MaterialDesign Toast A lightweight Toast library written with Kotlin i

Apr 22, 2022

An Android Toast replacement, similar to the one seen in the GMail app.

MessageBar An Android Toast replacement, similar to the one seen in the GMail app. Multiple messages can be posted in succession, and each message wil

Feb 16, 2022

Context sensitive notifications for Android

Context sensitive notifications for Android

Crouton Context sensitive notifications for Android DEPRECATION NOTICE This library has passed it's prime and is now considered deprecated. With the A

May 11, 2022

In-layout notifications. Based on Toast notifications and article by Cyril Mottier (http://android.cyrilmottier.com/?p=773).

In-layout notifications. Based on Toast notifications and article by Cyril Mottier (http://android.cyrilmottier.com/?p=773).

Android AppMsg (Crouton) Library Implementation of in-layout notifications. Based on Toast notifications and article The making of Prixing #4: in-layo

May 19, 2022

Android Custom Toast

Android Custom Toast

Super Toast Library Written Purely in Kotlin ❤️ Usual Toast but with super powers!!! 💪 A Fully Customised and Customisable Toast. ]( https://android-

May 2, 2021

Android Toast For RTL Applications

Android Toast For RTL Applications

RTL-Toast Android library to show Toasts in a pretty RTL way Install Add it in your root build.gradle allprojects { repositories { ... ma

Oct 19, 2021

CuteToast is an Material Design Custom Toast for Android | Custom Material Design Toast

CuteToast is an Material Design Custom Toast for Android | Custom Material Design Toast

CuteToast is an Android Custom Toast library that could be used instead of Default Toast. It does everything as Toast but with some extra spice.

Dec 14, 2021
Custom toasts with color and icon for Android.
Custom toasts with color and icon for Android.

Dynamic Toasts A simple library to display themed toasts with icon and text on Android 2.3 (API 9) and above devices. Since v0.4.0, it uses 26.x.x sup

May 10, 2022
Pretty material design toasts with feedback animations
Pretty material design toasts with feedback animations

Load Toast Library The default toasts are ugly and don't really provide much more than a short message. This small library provides a better toast whi

May 10, 2022
Balloon 🎈 A lightweight popup like tooltips, fully customizable with arrow and animations.
Balloon  🎈 A lightweight popup like tooltips, fully customizable with arrow and animations.

Balloon ?? A lightweight popup like tooltips, fully customizable with arrow and animations.

May 20, 2022
Toastie is a customizable Android toast library.
Toastie is a customizable Android toast library.

Toastie Getting Started Gradle Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories: Note

Apr 3, 2021
Attractive, stylish and customizable toast library for Android.

FabToast min SDK 16 (Jelly Bean 4.1) written in Java To download the demo app for this library from Google Playstore so you can see it in action, clic

Feb 14, 2022
Customizable toast message library for Android
Customizable toast message library for Android

Android Custom Toast Message (SnToast) Customizable Toast Message Library For Android Add this in your root build.gradle file allprojects { reposito

Nov 15, 2021
Android : IamToast Another Toast library for Android
 Android : IamToast  Another Toast library for Android

Android : IamToast Another Toast library for Android Warning. toast custom view is deprecated since android 11(R) Setup allprojects { repositories

Feb 7, 2022
A library that extends the Android toast framework.
A library that extends the Android toast framework.

SuperToasts Library The SuperToasts library enhances and builds upon the Android Toast class. This library includes support for context sensitive Supe

May 13, 2022
🍞 The missing toast library for Android.
🍞 The missing toast library for Android.

Literally Toast ?? A toast library for Android. Usage: ?? Use the LitToast to get lit and show your users a proper toast. LitToast.create(context, "My

Dec 17, 2021
A tooltip/showcase library for Android re-written in Kotlin.

UglyTooltip Based on another Showcase library. Being kotlinized, customized and refactored.

Apr 27, 2022