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 10, 2014 · 22 revisions

Screenshot

Introduction

SuperActivityToasts are made to replace stock Android Toasts that are used in an Activity Context.

Usage

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

Purpose

SuperActivityToasts attach themselves to the activity's content and will not linger from activity to activity.
SuperActivityToasts can have buttons, progressbars, and icons.

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

A simple SuperActivityToast displaying the text "Hello world!"
SuperActivityToast.createSuperActivityToast(Activity.this, "Hello world!", SuperToast.Duration.LONG).show();

A blue styled SuperActivityToast displaying the text "Hello world!" with a different animation
SuperActivityToast.createSuperActivityToast(Activity.this, "Hello world!",
    SuperToast.Duration.SHORT, Style.getStyle(Style.BLUE, SuperToast.Animations.FLYIN)).show();

A SuperActivityToast displaying the text "Hello world!" with a specified background and text color. This SuperActivityToast will also 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.TRANSLUCENT_PURPLE);
superActivityToast.setTextColor(Color.WHITE);
superActivityToast.setTouchToDismiss(true);
superActivityToast.show();

An indeterminate SuperActivityToast displaying the text "Hello world!" with an indeterminate progressbar.
SuperActivityToast superActivityToast = new SuperActivityToast(Activity.this, SuperToast.Type.PROGRESS);
superActivityToast.setText("Hello world!");
superActivityToast.setIndeterminate(true);
superActivityToast.setProgressIndeterminate(true);
superActivityToast.show();

A SuperActivityToast displaying the text "Hello world!" with a button (resembles undobar 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();

To use the button in BUTTON type SuperActivityToasts, define an OnClickWrapper somewhere in your Activity.
The first parameter is a string tag unique to this OnClickWrapper, the second parameter is the OnClickListener.

OnClickWrapper onClickWrapper = new OnClickWrapper("superactivitytoast", new View.OnClickListener() {

    @Override
    public void onClick(View view) {

        /** On click event */

    }

});

Orientation change support

To have SuperActivityToasts retain themselves on orientation change add this code to your Activity.
NOTE: If you do not call SuperActivityToast.onSaveState(); you may have lingering SuperActivityToasts when a new Activity is started on top of a showing SuperActivityToast. @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. <br>
```java
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