The Android Arsenal – Toasts

0
6

An android library to make customisable floating animated toasts

Getting Started

In your build.gradle

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

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.


LEAVE A REPLY

Please enter your comment!
Please enter your name here