Skip to content

SuperCardToast

Anatoliy Shuba edited this page Mar 23, 2014 · 15 revisions

Screenshot

##Introduction SuperCardToasts are made to augment the stock Android Toast class. The idea for this SuperToast came from Benjamin Weiss's Crouton library.

Usage

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

Purpose

SuperCardToasts offer a new way of showing toast messages.
SuperCardToasts attach themselves to the activity's content and will not linger from activity to activity.
SuperCardToasts can have buttons, progressbars, and icons.

Features

  1. Customization options such as background color, font, text size, animations etc.
  2. SuperCardToasts are stackable.
  3. Swipe/touch to dismiss options.
  4. Does not hide any of the activity's existing content.
  5. Duration can be any millisecond value.
  6. Orientation change support

Examples

You must place this layout in the top of your activity's layout. This is where SuperCardToasts will be attached. If you are using fragments you should put this at the top of your host activity's layout. Try setting the background color of this layout to match your ActionBar.

<LinearLayout  
    android:id="@+id/card_container"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:background="#15000000"  
    android:orientation="vertical" />

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

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

A SuperCardToast displaying the text "Hello world!" with a different background, text color, and will dismiss if the user swipes it.
SuperCardToast superCardToast = new SuperCardToast(Activity.this);
superCardToast.setText("Hello world!");
superCardToast.setDuration(SuperToast.Duration.LONG);
superCardToast.setBackground(SuperToast.Background.PURPLE);
superCardToast.setTextColor(Color.WHITE);
superCardToast.setSwipeToDismiss(true);
superCardToast.show();

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

A SuperCardToast displaying the text "Hello world!" with a button
SuperCardToast superCardToast = new SuperCardToast(Activity.this, SuperToast.Type.BUTTON));
superCardToast.setDuration(SuperToast.Duration.EXTRA_LONG);
superCardToast.setText("Some action performed.");
superCardToast.setButtonIcon(SuperToast.Icon.Dark.UNDO, "UNDO");
superCardToast.setOnClickWrapper(onClickWrapper);
superCardToast.show();

To use the button in BUTTON type SuperCardToasts, 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("supercardtoast", new SuperToast.OnClickListener() {

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

        /** On click event */

    }

});

Orientation change support

To have SuperCardToasts retain themselves on orientation change add this code to your Activity.

Important!

If you do not call SuperCardToast.onSaveState() you may have lingering SuperCardToasts when a new Activity is started on top of a showing SuperCardToast.

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

    SuperCardToast.onSaveState(outState);

}

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

SuperCardToast.onRestoreState(savedInstanceState, Activity.this);

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);
SuperCardToast.onRestoreState(Activity.this, savedInstanceState, wrappers);
You can’t perform that action at this time.