Skip to content
This repository has been archived by the owner on Jun 30, 2024. It is now read-only.

SuperActivityToast

John Persano edited this page Feb 22, 2014 · 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.setBackgroundResource(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 SuperActivityToast.onSaveState() 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
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    SuperActivityToast.onSaveState(outState);

}

Also, add this restore method to the onCreate of your Activity if you are NOT using any OnClickWrappers/OnDismissWrapers.

SuperActivityToast.onRestoreState(Activity.this, savedInstanceState);

If you ARE using any OnClickWrappers/OnDismissWrapers you must use the following code instead.
The Wrappers object helps to reattach any OnClickWrappers/OnDismissWrapers on orientation change so any OnClickWrappers/OnDismissWrapers used must be added to it.

Wrappers wrappers = new Wrappers();
wrappers.add(onClickWrapper);
SuperActivityToast.onRestoreState(Activity.this, savedInstanceState, wrappers);
Clone this wiki locally