Stale Android Toasts made tasty.

Related tags

Layout frenchtoast
Overview

FrenchToast

Stale Android Toasts made tasty.

Android Toasts are amazing, but they have a few major drawbacks:

  • You cannot control when they show up as well as their duration. Other apps can enqueue toasts that will delay yours from showing up.
  • They break context: they remain on screen when the user switches to other activities.
  • The API is error prone: Toast.makeText(context, "Important Toast", LENGTH_LONG); // Don't forget show()!

FrenchToast gives you absolute control over your app Toasts. It does so by duplicating the internals of Android Toasts and giving you access.

Unlike other Toast-like libraries, FrenchToast doesn't add a view to the root of your activity. Instead, it creates a new Window for each Toast, exactly like the real Android Toasts.

demo.gif

Getting Started

In your build.gradle:

 dependencies {
   implementation 'info.piwai.frenchtoast:frenchtoast:1.0'
 }

You need to setup FrenchToast in your Application class:

public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    FrenchToast.install(this);
  }
}

You are ready to Toast!

FrenchToast.with(context).showText("I love Baguettes!");

A FrenchToast:

  • hides when the Activity is paused,
  • shows again when it's resumed,
  • has a default duration of Toast.LENGTH_LONG,
  • survives configuration changes,
  • is queued, so that only one Toast shows at once.

You can customize the default duration:

FrenchToast.with(context).shortLength().showText(R.string.short_bread);

FrenchToast.with(context).longLength().showText(R.string.long_bread);

FrenchToast.with(context).length(3, SECONDS).showText(R.string.bespoke_bread);

The duration of a Toast resets when the activity is paused / resumed, to make sure the user had enough time to see the Toast.

Bespoke Toasts

A Toast can be created from a layout:

FrenchToast.with(context).showLayout(R.layout.toasted_baguette);

You can also dip an Android Toast:

Toast toast = Toast.makeText(context, "BREAD ALL THE THINGS!", LENGTH_SHORT);
toast.setGravity(LEFT | TOP, 0, 0);
FrenchToast.with(context).showDipped(toast);

Unplugging the Toaster

A Toast can be canceled:

Toasted toasted = FrenchToast.with(context).showText("I love Baguettes!");
// I'd rather show a Bagel.
toasted.cancel();

You can also clear all queued Toasts for a given Activity:

FrenchToast.with(context).clear();

Context vs Activity

FrenchToast.with() takes a Context, however it expects that Context to be an Activity or to wrap an Activity, because FrenchToast keeps one ToastQueue for each activity.

Crafting your own Mixture

If you want more control over when to show / hide Toasts, you can directly use Mixture:

Toast toast = Toast.makeText(context, "BREAD ALL THE THINGS!", LENGTH_SHORT);
Mixture mixture = Mixture.dip(toast);
// The Toast is shown forever, as long as the process lives:
mixture.show();
// Or until you call hide:
mixture.hide();

FAQ

What's up with the strange names?

A French toast is a dish made of bread dipped in a mixture of beaten eggs and then fried.

Is this a serious project?

Yes. Despite the puns, this code is production ready. It is heavily inspired from android.widget.Toast.

What's the minimum supported Android?

FrenchToast requires a minimum SDK version of 14 or above, because it uses Application.ActivityLifecycleCallbacks.

Why reimplement Toast?

Because we can have better Toasts, so we should.

I read the source of Toast when I was flying back, still under a creative influence of Droidcon NYC. I realized it could be done, so I wrote FrenchToast.

logo.png

License

Copyright 2015 Pierre-Yves Ricau

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
A very simple arc layout library for Android
A very simple arc layout library for Android

ArcLayout A very simple arc layout library for Android. Try out the sample application on the Play Store. Usage (For a working implementation of this

Android layout that simulates physics using JBox2D
Android layout that simulates physics using JBox2D

PhysicsLayout Android layout that simulates physics using JBox2D. Simply add views, enable physics, and watch them fall! See it in action with the sam

Android component which presents a dismissible view from the bottom of the screen
Android component which presents a dismissible view from the bottom of the screen

BottomSheet BottomSheet is an Android component which presents a dismissible view from the bottom of the screen. BottomSheet can be a useful replaceme

This library provides a simple way to add a draggable sliding up panel (popularized by Google Music and Google Maps) to your Android application. Brought to you by Umano.
This library provides a simple way to add a draggable sliding up panel (popularized by Google Music and Google Maps) to your Android application. Brought to you by Umano.

Note: we are not actively responding to issues right now. If you find a bug, please submit a PR. Android Sliding Up Panel This library provides a simp

Allows the easy creation of animated transition effects when the state of Android UI has changed
Allows the easy creation of animated transition effects when the state of Android UI has changed

android-transition Android-Transition allows the easy creation of view transitions that reacts to user inputs. The library is designed to be general e

waveview for android
waveview for android

中文介绍 WaveView A view to display wave effect. Screenshot Integration implementation 'com.gelitenight.waveview:waveview:1.0.0' Setter methods: setWaveS

An Android demo of a foldable layout implementation. Engineered by Vincent Brison.
An Android demo of a foldable layout implementation. Engineered by Vincent Brison.

Foldable Layout This code is a showcase of a foldable animation I created for Worldline. The code is fully written with java APIs from the Android SDK

Flexbox for Android
Flexbox for Android

FlexboxLayout FlexboxLayout is a library project which brings the similar capabilities of CSS Flexible Box Layout Module to Android. Installation Add

A floating menu library for Android.
A floating menu library for Android.

Hover Hover is a floating menu implementation for Android. Goals The goals of Hover are to: Provide an easy-to-use, out-of-the-box floating menu imple

Comments
  • Annotate Methods with @MainThread and @NonNull

    Annotate Methods with @MainThread and @NonNull

    Do you think it would be worthwhile to include the @MainThread or @NonNull annotations when a method is required to be invoked on the main thread or a method's argument must be non-null, respectively? This may help developers catch errors at compile time that would result in an Exception to be thrown at runtime. Of course, this would require adding the Android support annotations library as a dependency.

    opened by jpetitto 3
  • Specify min SDK version in readme

    Specify min SDK version in readme

    I found out upon using FrenchToast that it requires a min SDK level of 14 or above. Thought it might be helpful to include this information in the readme. I added it to the FAQ section; does that make sense to you?

    opened by jpetitto 1
  • Annotate public API with Android's support annotations

    Annotate public API with Android's support annotations

    This is in reference to issue #4:

    The latest version of the support annotations library has been added as a dependency. Methods that require invocation on the main thread are annotated with @MainThread. Likewise, any arguments that require a non-null value are annotated with @NonNull. I've also applied @StringRes and @LayoutRes to arguments that are expected to be resource IDs.

    opened by jpetitto 0
Owner
py - Pierre Yves Ricau
🍷🥖⛷🇫🇷
py - Pierre Yves Ricau
Android library used to create an awesome Android UI based on a draggable element similar to the last YouTube graphic component.

Draggable Panel DEPRECATED. This project is not maintained anymore. Draggable Panel is an Android library created to build a draggable user interface

Pedro Vicente Gómez Sánchez 3k Jan 5, 2023
Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your development.

Bubbles for Android Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your

Txus Ballesteros 1.5k Jan 2, 2023
Android library used to create an awesome Android UI based on a draggable element similar to the last YouTube New graphic component.

Please switch to DragView, for the best support, thank you DraggablePanel Download allprojects { repositories { ... maven { url 'https://jitp

Hoàng Anh Tuấn 103 Oct 12, 2022
FixedHeaderTableLayout is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells with scrolling and zooming features. FixedHeaderTableLayout is similar in construction and use as to Android's TableLayout

FixedHeaderTableLayout is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells with scrolling and zooming features. FixedHeaderTableLayout is similar in construction and use as to Android's TableLayout

null 33 Dec 8, 2022
A wave view of android,can be used as progress bar.

WaveView ![Gitter](https://badges.gitter.im/Join Chat.svg) A wave view of android,can be used as progress bar. Screenshot APK demo.apk What can be use

Kai Wang 1.3k Dec 28, 2022
An Android Layout which has a same function like https://github.com/romaonthego/RESideMenu

ResideLayout An Android Layout which has a same function like https://github.com/romaonthego/RESideMenu. Can be used on Android 1.6(I haven't try it.)

Yang Hui 392 Oct 12, 2022
An Android library that help you to build app with swipe back gesture.

SwipeBackLayout An Android library that help you to build app with swipe back gesture. Demo Apk GooglePlay Requirement The latest android-support-v4.j

ike_w0ng 6.1k Dec 29, 2022
TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.

This project isn't maintained anymore. It is now recommended to use https://github.com/peterLaurence/MapView. MapView is maintained by Peter, one of o

Mike Dunn 1.5k Nov 21, 2022
Ultra Pull to Refresh for Android. Support all the views.

Welcome to follow me on GitHub or Twitter GitHub: https://github.com/liaohuqiu Twitter: https://twitter.com/liaohuqiu 中文版文档 Wanna auto-load-more? This

Huqiu Liao 9.6k Jan 5, 2023
SwipeBack is an android library that can finish a activity by using gesture.

SwipeBack SwipeBack is a android library that can finish a activity by using gesture. You can set the swipe direction,such as left,top,right and botto

Eric 1.7k Nov 21, 2022