Fork us on GitHub

ToastBar Messages

Replacing everything with Toasts
ToastBar Messages

ToastBar Messages

The ToastBar was one of those API’s I didn’t know I needed and yet I became addicted to it…​
Ever since Steve came out with the ToastBar I constantly catch myself typing only to delete that to use the ToastBar. It’s both really easy to use and more consistent with modern mobile UI design.

As a replacement for Dialog it really needed the static "show methods", so we added a simpler showErrorMessage which combined the ToastBar with the material font icons to create a proper error message. This allowed us to show an error message with a single line of code, but these things shouldn’t be used only for errors which is why we just added two new static methods: showMessage(String msg, char icon, int timeout) & showMessage(String msg, char icon).

The icon argument is one of the FontImage constants representing the material design icons. This allows us to show simpler notifications e.g. file downloaded successfully etc.

We can now do this using code such as this:

Form f = new Form("Toast", BoxLayout.y());

Button showError = new Button("Show Error");
Button showNotification = new Button("Show Notification");
Button showWarning = new Button("Show Warning");

showError.addActionListener(e -> ToastBar.showErrorMessage("This is an error"));
showNotification.addActionListener(e -> ToastBar.showMessage("This is a notification", FontImage.MATERIAL_INFO));
showWarning.addActionListener(e -> ToastBar.showMessage("This is a warning!", FontImage.MATERIAL_WARNING));

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.