android – How can I include a button in a Toast notification?-ThrowExceptions

Exception or error:

First of all, I know this has been asked before: Button in custom Android Toast?.
This is nearly an exact duplicate, but I think that it warrants a new question based on the fact that it’s been used in apps, namely Gmail for ICS (it appears when you delete a message).

The linked question says that it’s not possible to include a button in a Toast because Toasts cannot be focused. Is this wrong, outdated, or did Gmail find a way around it?

How to solve:

The Gmail undo bar is’t a toast, here is how Google did it

http://code.google.com/p/romannurik-code/source/browse/misc/undobar/src/com/example/android/undobar/UndoBarController.java

I guess this answers your question.

###

What you’re referring to is not a Toast but what Google has dubbed a Snackbar. See the Material Design guidelines.

You can find several implementations of this on GitHub. Some also go by the name of UndoBar

Currently, the most extensive, popular, and active one seems to be Snackbar by nispok, which I also happen to be using.

###

If you want a button in a toast, its better you quit that idea. But you can use dialogs in place of toast. Using dailogs, you will be able to display whatever you want (same function as a toast would do). Also in the dailog, you could set buttons.

###

Gmail on iOS does provide a toast and it is a much better solution than interrupting the user flow with a dialog.

![Toast style message in Gmail for iOS][1] [1]: http://i.stack.imgur.com/LWClq.jpg

If you use this option, make sure the toast is displayed long enough for the user to tap undo if needed. So 5 seconds as opposed to 2.5-3 seconds in a info-only toast.

The other example cited by CommonsWare is the inline feedback which is shown after a swipe gesture. This is not a toast- but it is also a great way to provide feedback that an action has been performed.

###

I have extended the UndoBar mentioned by @Hazem (link) and made it more generic so that it can be used for other actions also. You can have a look here.

Leave a Reply

Your email address will not be published. Required fields are marked *