• Stars
    star
    363
  • Rank 117,374 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Stale Android Toasts made tasty.

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.

More Repositories

1

fragnums

An enum based library to replace fragments.
Java
578
star
2

shipfaster

Dagger + Otto + Retrofit + Robolectric + Picasso + OkHttp
Java
328
star
3

CleanAndroidCode

ALL YOUR LIB ARE BELONG TO US
Java
173
star
4

androidsrc

Find Android sources
Ruby
173
star
5

SharkApp

Kotlin
42
star
6

diy

Kotlin
40
star
7

BuilderGen

Automatically generated builders
Java
31
star
8

pyricau.github.com

p-y.wtf static website
30
star
9

CatchLeaks

Bring your laptops and your investigative skills, we're going on a memory leak hunt!
Kotlin
29
star
10

dagger2-mortar-flow-experiment

Experimental hack to get Dagger 2, Mortar and Flow to work together
Java
26
star
11

FunkyJFunctional

A funky way to use functional programming in Java
Java
21
star
12

simpleperf-cleanup

Kotlin
18
star
13

androidannotations-dagger-example

An example of how to integrate AndroidAnnotations with Dagger. Experimental.
Java
16
star
14

toohardforyou

2H4U, which stands for Too Hard For You, is a mix between a Tetris-like game and a wall breaker.
Java
14
star
15

rockslide

Rockslide generates dynamic presentations
JavaScript
13
star
16

handler-thread-leaks

A simple projects that reproduces HandlerThread leaks
Kotlin
9
star
17

slides-formation-gwt

GWT Webapp based slides for the GWT training
Java
6
star
18

neveridle

Kotlin
5
star
19

TechTrekSummer2014

An app that consumes data from govtrack.us
Java
4
star
20

bisouland

Jeu par navigateur, volez des points d'amour à vos adversaires en leur envoyant des bisous !
PHP
4
star
21

waste-o-meter

Android application to compute the cost of waiting for your computer
Java
3
star
22

express-board-android

Repository for eXpress JobZ, an Android client for two french Job Boards
Java
3
star
23

FormationAndroid

Repo pour les TP de la formation Android
Java
3
star
24

DevoxxBeerCounter

App for AA talk at Devoxx 2012
Java
2
star
25

excilys-teaser-formation-gwt

Mini site pour les inscriptions à la formation GWT
2
star
26

cv-piwai

My online GWT Curriculum Vitae
Java
2
star
27

maven-forplay-archetype

A Maven archetype to quickly get started with ForPlay
Java
2
star
28

leakcanary-shell

An empty shell app to inject LeakCanary into other APKs
2
star
29

forplay-clone-pyricau

Clone to test forplay
Java
2
star
30

greenhouse-androidannotations

Porting greenhouse-android to AndroidAnnotations
Java
1
star
31

chromium-webview-leak

Repo that reproduces a Chromium Webview memory leak
Kotlin
1
star
32

androidannotations-scoping

A fake Android project that demonstrate how scoping would look like if implemented in AndroidAnnotations
Java
1
star
33

french-keyboard-putain

Type French in One Word
Java
1
star
34

fosdem-sms

Java
1
star
35

deRPC-big-payload

Sample demo GWT project to show how deRPC can generate big payloads
1
star
36

Formalys

App liée à la gestion des formations Excilys
Java
1
star
37

BugAptEclipse

Reproduces a bug in Eclipse + APT
Java
1
star
38

slides-gwt-android-intro

Slides for a GWT & Android presentation at INSA Rouen
Java
1
star
39

speakerz

Java
1
star
40

maven-aa-apt-eclipse

Sample project using AA and annotation processing, trying to set everything right on Eclipse.
Java
1
star