1

I want to make toast click able or a UI element which will have clickable button but behaves like toast. It should not run on UI thread like toast. It should not halt or overlay current user activity, message should come like toast with clickable button and vanish but as toast user should be able to access background ongoing UI item.

If any one has any idea about how to achieve this pls share with me.

om252345
  • 2,395
  • 4
  • 29
  • 31

4 Answers4

3

Little trick. Tested working Android 4.4

toast = new Toast(context);
try {
    Class<?> clazz = Class.forName("android.widget.Toast");
    Method method = clazz.getDeclaredMethod("getWindowParams");
    WindowManager.LayoutParams param = (WindowManager.LayoutParams) method.invoke(toast);
    param.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
} catch (Exception e) {
    e.printStackTrace();
}
You Kim
  • 2,355
  • 1
  • 10
  • 11
2

The Gmail undo bar is the best suitable for you, its just like a toast with a button. Here is a code implementation for it.

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

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

I had a similar requirement that I solved using a PopupWindow. Basically, I had an about window with a clickable link that I wanted displayed like a toast. The popup window can accomplish this as follows:

In the parent class, I use the following flag:

private boolean durationExpired = false;

Then, when I invoke what would have been the toast, I do the following instead:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.about_hiittimer,
    (ViewGroup) findViewById(R.id.about_hiittimer));
TextView url = (TextView) layout.findViewById(R.id.url);
url.setMovementMethod(LinkMovementMethod.getInstance());

final PopupWindow popupWindow = new PopupWindow(layout, 280, 160, false);
popupWindow.showAtLocation(layout, 17, 0, 0);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);

final Handler popupHandler = new Handler();
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        if (!durationExpired) {
            durationExpired = true;
            popupHandler.postDelayed(this, 2000);
        } else {
            popupWindow.dismiss();
            popupHandler.removeCallbacks(this);
            durationExpired = false;
        }
    }
});
user3570982
  • 559
  • 1
  • 6
  • 14
1

Put your main layout inside a FrameLayout. Write a layout for your toast, with the button and all, insert it into the FrameLayout (below your main layout) and set its visibility to GONE.

When you show it (setting visibility to VISIBLE) start a new thread that counts down the seconds till its dismissed. Set it back to invisible from the thread via a Handler (cause all UI elements can only be managed from the main thread).

cheers!

pumpkee
  • 3,357
  • 3
  • 27
  • 34
  • I want a UI element like toast will give access to ongoing activity while showing message. Like if I am playing a game or typing or viewing something on market, Toast message will not restrict me to access this ongoing work, do not pause game but shows message at the same time. Toast has not item clickabke but I want to make same functionality as toast but having a button clickable on it... – om252345 May 07 '11 at 06:05
  • Yes, and this is exactly what this does. It shows a view on top of your main layout. Nothing is stopped, everything is still accessable (except what ever lies directly underneath the new view of course). But you might check out the PopupWindow too. I never heard of it before, but it looks like a much more conveniant way to do what you want. – pumpkee May 07 '11 at 06:13
  • Of course that won't work. It'll be inside your activity (if you even have an activity), and won't show up when another application is running, which is the whole point of toasts. – Glenn Maynard Apr 28 '13 at 19:28