Is it possible to have a button in a Toast?
In theory, yes because you can build a custom Toast from a layout in XML, but I tried to put a button in it and couldn\
You can try SuperToast in this case. It can create toast with button. It has custom duration feature, colourful background, colourful fonts, custom fonts, animated effect. Hope u will enjoy it
You should use a Snackbar
. It is in the latest android support library(at time of answer) and is compatible with older api levels. It is much easier to implement than a Dialog
or custom View
and has the ability to have a button unlike a Toast
.
Android Support Library
from Extras
in the SDK Manager
(revision 22.2.1 or later).build.gradle
add this to the class dependencies: com.android.support:design:22.2.0
.Implement:
Snackbar.make(this.findViewById(android.R.id.content), "Toast Message", Snackbar.LENGTH_LONG)
.setAction("Click here to activate action", onClickListener)
.setActionTextColor(Color.RED)
.show;
And that is it. No github projects and implementation is very similiar to Toast
. I used it in one of my projects and it works great.
Creating a system overlay window (always on top)
This is suggesting that it can be done, I also need buttons in a toast so I still have to make my own implementation. If I find more I will add it to my post
Use an alertbox, if you want to add a button :-). Here are some examples Dialog boxes in Android
A custom view passed to a toast can contain anything; however, toasts cannot receive any touch events so no components that use touch events will work in a stock toast (buttons, radiobuttons, etc.). The only choice you have is to create a custom view with a button in it and add it to your layout. There are many examples of how to do this and a few libraries you can check out to see how other people are doing it.
UndoBar
MessageBar
Nurik's UndoBar
Of course you are also welcome to use the SuperToasts library I put together however it might be a little overkill for one usage. The way that I do it is outlined in the SuperActivityToast class.
A toast can not be clicked. It is not possible to capture a click inside a toast message. You will need to build a dialog for that. Look at Creating Dialogs for more info.
The API on the Toast class state that a toast will never receive the focus and because a toast is not a view there is no onClick message. I would assume that therefore childs of a Toast can not be clicked as well.