Android Toaster - Toasts and Dialogs


Toaster is an android library for developing Toasts and Dialogs.

The library is written by Alexey Korolov and is available as an open source project under Apache License.

Uses of Toaster

Toaster has two main functions:

No. Use
1. To show Toasts.
2. To show Dialogs.

Advantages of Toaster

These are some of the reasons to use Toaster:

No. Advantage
1. Ease of use
2. Simple and Customizable.
3. Allows us create Toast messages and Dialogs using one class

Programmatic Definition

Toaster like all java classes is defined in a package:

package net.alexandroid.utils.toaster;

As a class Toaster implements the View.OnClicklistener interface:

public class Toaster implements View.OnClickListener {}

Toasts

Toasts normally display messages over a small duration of time before disappering automatically.

Here's an example of a Toast with Toaster:

Toaster.showToast(MainActivity.this,"IC1011 is the largest Galaxy ever discovered");

How the Toast Works under the hood.

Toaster class defines two showToast() methods with various parameters . These two methods all require us to pass a Context object.

Internally that Context object will be used during the inflation of the layout to be displayed for your Toast.

Inflation is the process of converting an XML layout into a View object. This is done by an abstract class called LayoutInflater, that resides in the android.view package.

A layout is defined inside the Toaster library with a TextView inside a FrameLayout. That TextView is what is used to show our messages.

For animation Toaster uses ObjectAnimator, a final class defined in the android.animation package.

Apart from the Context object our showToast() method can take other parameters as listed below:

Method Parameters
showToast() Context ctx; String msg;
showToast() Context cts; String msg; int animationDuration; int visibleDuration;

Those are the two methods used to show Toast message.

How Toasts's Showing and Automatic Disappering Works.

We all know that Toasts show for a small duration then automatically disappear. How does this work?

Well Toaster uses a class called ObjectAnimator for this. This class is basically an animation class.

ObjectAnimator derives from android.animation.ValueAnimator.

ObjectAnimator provides the capability to animate properties on target objects.

Toaster will start an animation for the specified duration.

It will will then raise a callback when the animation ends. When this happens we remove the view containing our message from a ContentFrameLayout where we had added it:

 fadeOut.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                contentFrameLayout.removeView(layout);
            }
        });

Toaster Dialogs

Toaster allows us show dialogs as well.

Here's an example of showing a Dialog using Toaster:

        Toaster toaster=new Toaster.Builder(this)
                .setTitle("Galaxies")
                .setText("The Largest Galaxy in The Universe is IC 1011")
                .setPositive("Got it")
                .setNegative("Cancel")
                .setAnimationDuration(300)
                .setCallBack(new Toaster.DialogCallback() {
                    @Override
                    public void onPositiveClick() {
                        Toaster.showToast(MainActivity.this,"OK Clicked");
                    }

                    @Override
                    public void onNegativeClick() {
                        Toaster.showToast(MainActivity.this,"Cancel Clicked");
                    }
                }).build();
        toaster.show();

How Toaster Dialogs Work under the Hood

Toaster class defines an inner static Builder class.

public static class Builder {...}

Builder classes simplify our work when we need to invoke several methods from a single class.

Each method once, its been invoked and done its job, returns the instance of the class.

Thus this allows us to use that instance to further invoke another method, which also returns the instance and so forth.

Internally this Builder class first maintains a Toaster reference as a private field.Remember The Builder class resides in the Toaster class.

The Builder class has one constructor, a public one where the following take place:

No. Description
1. An activity reference is passed via as a parameter.
2. The Toaster reference gets instantiated here.
3. A WeakReference defined in the Toaster class is instantiated here, passing in the activity reference.
4. The dialog animation is set.

The following Dialog manipulation methods are defined:

No. Method Return Type Description
1. setTitle(string title) Builder Sets dialog title and returns Builder instance
2. setText(string text) Builder Sets dialog text and returns Builder instance
3. setPositive(string positive) Builder Sets dialog's positive button text and returns Builder instance
4. setNegative(string negative) Builder Sets dialog's negative button text and returns Builder instance
5. setCallback(DialogCallback callback) Builder Sets dialog's callback and returns Builder instance
6. setCustomLayout(int customLayout) Builder Sets dialog's custom layout and returns Builder instance
7. setAnimationDuration(int animationDuration) Builder Sets dialog's animation duration and returns Builder instance
8. build() Toaster Builds the dialog and returns Toaster instance

Dialog Callbacks

Toaster dialogs provide two callbacks, onPositiveClick() and onNegativeClick().

These callbacks are defined in an interface called DialogCallback inside the Toaster class:

    public interface DialogCallback {
        void onPositiveClick();

        void onNegativeClick();
    }

Showing and Hiding Dialogs

Toaster defines two method for showing or hiding dialog:

Method Role
show() Show Dialog
hide() Hide Dialog

What happens to dialog when Back button is pressed

When a user presses the back button of the device, internally we determine whether the dialog is visible or not.

This is possible because of two reasons:

No. Reason
1. First we've overidden the onBackPresed() internally method in our Toaster class.
2. Our Toaster class maintains a static boolean data member. That variable gets set to true when a dialog is shown and false when the dialog is dismissed. We check it's value inside the onBackPresse() method and if hide the dialog if it's visible.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post