19

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?

Community
  • 1
  • 1
jmgrosen
  • 1,030
  • 2
  • 10
  • 18
  • You could use a dialog anyway... – androidavid Mar 05 '12 at 23:11
  • 4
    What `Toast` in Gmail are you referring to? And, more to the point, how did you prove that it is a `Toast`? – CommonsWare Mar 05 '12 at 23:15
  • 1
    I've just tried Gmail on ICS and I couldn't find in what case it gives a toast with a button in it. Besides with a custom Dialog, or a custom transparent Activity with Dialog on it, it should be really easy to mimic the look and behavior of a Toast which is virtually indistinguishable from the real thing. – Stephan Branczyk Mar 06 '12 at 00:08
  • @CommonsWare Hmm... good point. I suppose I should be more sure that it's actually a `Toast` first. – jmgrosen Mar 06 '12 at 00:23
  • 2
    "a Toast appears with text and an "Undo" button" -- that's not a `Toast`. For starters, it is there indefinitely, or until you touch something else, whereas a `Toast` vanishes after a period of time. If I had to guess, that is a simple `ViewGroup` floating over top the main activity (by being a later child in a `RelativeLayout`, perhaps). – CommonsWare Mar 06 '12 at 00:26
  • @CommonsWare OK - thanks for your help! I probably should have researched this problem before asking it so quickly. You should contribute that as an answer because although it doesn't strictly qualify as a `Toast`, to the user it looks like a `Toast` for all intents and purposes (I was fooled!). – jmgrosen Mar 06 '12 at 00:28
  • My answer was deleted because I gave the same answer to multiple questions, but I think it would have really helped. Gmail uses a `Snackbar` for that. It is much easier than a custom `View` or `Dialog` and is very easy to implement. Check [here](http://www.truiton.com/2015/06/android-snackbar-example/) for setup tutorial and [here](http://developer.android.com/tools/support-library/features.html#design) for implementation. By the way @MartijnPieters I edited my question. I would appreciate if you look at it and undelete it. – Dylan Vander Berg Aug 01 '15 at 16:55
  • @MartijnPieters The accepted answer for this question also has the same answer as a [different question](http://stackoverflow.com/questions/3308975/button-in-custom-android-toast/12025517#12025517). – Dylan Vander Berg Aug 02 '15 at 01:54

5 Answers5

36

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.

Hazem Farahat
  • 3,790
  • 2
  • 26
  • 42
2

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.

Divisible by Zero
  • 2,616
  • 28
  • 31
1

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • 3
    Although a dialog _would_ work in my case, I'd much rather have a Toast because they are much less obtrusive -- going back to the Gmail example, a Toast appears with text and an "Undo" button, because it is unlikely that they would want to undo it. A dialog would become quite annoying if one popped up every time you wanted to delete messages. – jmgrosen Mar 06 '12 at 00:21
0

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]: https://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.

0

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.

Ashwani K
  • 7,880
  • 19
  • 63
  • 102