Toasts – or how to pop up a message for the user

Users of the Dublin Buzz application can add sights to their list of favourites. This makes them easily accessible at a later time. If you fancy popping down to a sight after reading about it, you can simple hit the “Make Fav” button on the sight’s page. When you’ve tired of it, you simply take it off your favourites list.

Both these actions – adding a favourite or taking it away – use a facility in Android called “Toasts”. They’re a fancy name for something incredibly simple – displaying a little pop-up bit of text on-screen for a short amount of time, before it automatically fades away. (See screen grab below). It is the perfect mechanism for alerting your users to something they’ve done, but without slowing them down by insisting the press “OK” on a dialog box. It is probably the least intrusive way to give them information and is perfect for making the application more user-friendly.

Here’s the code for producing toasts in Dublin Buzz:

 Toast toast = Toast.makeText(this, R.string.subFav, Toast.LENGTH_SHORT);
 toast.show();

Did I say it was incredibly simple?

A Toast object is constructed using the Toast classes’ makeText() method. This takes the current context (the activity, or “this”), a string to display, and the length of time to display it for. The alternative to Toast.LENGTH_SHORT is Toast.LENGTH_LONG (which is customisable). Then, once declared, you call toast.show() and it is displayed, asynchronously, so it doesn’t block.

And voila! You have a message displayed.

Advertisements
This entry was posted in Android, Android development, Mobile Apps. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s