FrenchToast


Source link: https://github.com/pyricau/frenchtoast

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.

Getting Started

In your build.gradle:

 dependencies {

 compile '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.

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. 

Resources

A simple, easy to use, no frills, form validator for Android.

This plugin makes it easy to search text in strings resources.

An RxJava 2 CallAdapter.Factory implementation for Retrofit 2.

Usb serial controller for Android.

HTML Content (Article) Extractor for Android - fork of Goose by Gravity Labs.

Adapter between RxJava V1 and V2.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes