Skip to content

SuperActivityToast

Pfennigbaum edited this page Jun 11, 2015 · 22 revisions

Screenshot

Introduction

SuperActivityToasts are made to replace stock Android Toasts that are used in an Activity Context.
SuperActivityToasts attach themselves to the activity's content and will not linger from activity to activity.
SuperActivityToasts can have buttons, progressbars, and icons.

Usage

SuperActivityToasts should be used wherever an Activity context is available.
SuperActivityToasts can NOT be used in a non-Activity context.

Features

  1. Customization options such as background color, font, text size, animation etc.
  2. Duration can be any millisecond value.
  3. An icon can be displayed along with the SuperActivityToast.
  4. SuperActivityToasts can recieve onClick() events.
  5. Touch to dismiss function.
  6. Four variations.
  7. Orientation change support

Examples

Below you will find code examples for the different types of SuperActivityToasts.

Standard


NOTE: Standard SuperActivityToasts are default, if a type is not specified your SuperActivityToast will be the standard type.


Static method to create and show a standard SuperActivityToast.

SuperActivityToast.create(Activity.this, "Hello world!", SuperToast.Duration.LONG).show();

Static method to create and show a standard SuperActivityToast with a specified pre-set style.

SuperActivityToast.create(Activity.this, "Hello world!",
    SuperToast.Duration.SHORT, Style.getStyle(Style.BLUE, SuperToast.Animations.FLYIN)).show();

New SuperActivityToast object styled via methods and will dismiss if the user touches it.

SuperActivityToast superActivityToast = new SuperActivityToast(Activity.this);
superActivityToast.setText("Hello world!");
superActivityToast.setDuration(SuperToast.Duration.LONG);
superActivityToast.setBackground(SuperToast.Background.PURPLE);
superActivityToast.setTextColor(Color.WHITE);
superActivityToast.setTouchToDismiss(true);
superActivityToast.show();

Progress

IMPORTANT: Progress SuperActivityToasts will NOT recreate on orientation change, this should be managed by the developer similar to a ProgressDialog.


New SuperActivityToast object with an indeterminate duration and progressbar.

SuperActivityToast superActivityToast = new SuperActivityToast(Activity.this, SuperToast.Type.PROGRESS);
superActivityToast.setText("Hello world!");
superActivityToast.setIndeterminate(true);
superActivityToast.setProgressIndeterminate(true);
superActivityToast.show();

Progress Horizontal

IMPORTANT: Progress Horizontal SuperActivityToasts will NOT recreate on orientation change, this should be managed by the developer similar to a ProgressDialog.


New SuperActivityToast object with an indeterminate duration and progressbar with set progress.

SuperActivityToast superActivityToast = new SuperActivityToast(Activity.this, SuperToast.Type.PROGRESS_HORIZONTAL);
superActivityToast.setText("Hello world!");
superActivityToast.setIndeterminate(true);
superActivityToast.setMaxProgress(100);
superActivityToast.setProgress(20);
superActivityToast.show();

Button


New SuperActivityToast object that resembles the undo delete bar in Gmail app.

SuperActivityToast superActivityToast = new SuperActivityToast(Activity.this, SuperToast.Type.BUTTON);
superActivityToast.setDuration(SuperToast.Duration.EXTRA_LONG);
superActivityToast.setText("Some action performed.");
superActivityToast.setButtonIcon(SuperToast.Icon.Dark.UNDO, "UNDO");
superActivityToast.setOnClickWrapper(onClickWrapper);
superActivityToast.show();

/**
 * The OnClickWrapper is needed to reattach SuperToast.OnClickListeners on orientation changes. 
 * It does this via a unique String tag defined in the first parameter so each OnClickWrapper's tag 
 * should be unique.
 */
OnClickWrapper onClickWrapper = new OnClickWrapper("superactivitytoast", new SuperToast.OnClickListener() {

    @Override
    public void onClick(View view, Parcelable token) {

        /* On click event */

    }

});

Orientation change support

To have SuperActivityToasts retain themselves on orientation change you must implement the following code.

Save state

IMPORTANT: If you do not call this method you may have lingering SuperActivityToasts when a new Activity is started on top of a showing SuperActivityToast.


Add the following to your Activity, this will save the SuperActivityToast that is showing along with any others in the queue.

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    SuperActivityToast.onSaveState(outState);

}

Restore state

IMPORTANT: If you have used any OnClickWrappers or OnDismissWrappers you must use the second method.


Add this restore method to the onCreate of your Activity, this will recreate and show any SuperActivityToasts showing or pending before the orientation change.

SuperActivityToast.onRestoreState(savedInstanceState, Activity.this);

If you are using any OnClickWrappers or OnDismissWrapers you must create a new Wrappers object, add any OnClickWrappers or OnDismissWrapers you used to the Wrappers object, and pass the Wrappers object as a parameter. This reattaches them to the appropriate SuperActivityToasts.

Wrappers wrappers = new Wrappers();
wrappers.add(onClickWrapper);
SuperActivityToast.onRestoreState(savedInstanceState, Activity.this, wrappers);
You can’t perform that action at this time.