0

I have to create a notification like an android toast notification but I need to throw it from a service and I need to close it when I want. standard toast notification would be perfect, but it's too much short.

I tried with a DialogFragment, but it takes the focus (not like toast) and I could not throw it from a service, but only from a FragmentActivity.

Thanks!!

user3906040
  • 651
  • 1
  • 8
  • 12
  • create your custom toast by extending the toast class and set the duration there – Pramod Yadav Nov 20 '14 at 10:43
  • http://stackoverflow.com/questions/2220560/can-an-android-toast-be-longer-than-toast-length-long Have you seen this question? The library crouton recommended looks like it meets your criteria – user1646196 Nov 20 '14 at 10:43
  • Check out the link below .. http://stackoverflow.com/questions/22806166/how-to-toast-a-message-for-a-specific-time-period – Kunal Nov 20 '14 at 10:48

2 Answers2

1
            Toast toast = new Toast(this);
            TextView textView=new TextView(this);
            textView.setTextColor(Color.BLUE);
            textView.setBackgroundColor(Color.TRANSPARENT);
            textView.setTextSize(20);
            textView.setText("My Toast For Long Time");
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

            toast.setView(textView);

          timer =new CountDownTimer(20000, 1000)
            {
                public void onTick(long millisUntilFinished)
                {
                    toast.show();
                }
                public void onFinish()
                {
                    toast.cancel();
                }

            }.start();
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

From a quick search in SO, you can find the Toast durations:

private static final int LONG_DELAY = 3500; // 3.5 seconds
private static final int SHORT_DELAY = 2000; // 2 seconds

Now you can display multiple Toasts one after the other to look like it has a longer duration:

final String msg = "Some text";
Runnable delayedToast = new Runnable() {
    @Override
    public void run() {
        Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
    }
};

Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show(); 
mHandler.postDelayed(delayedToast, 3000);
mHandler.postDelayed(delayedToast, 6000);

Where ctx is your activity/application context and mHandler is a handler on the UI Thread. The duration should be around 3000+3000+3500.

Community
  • 1
  • 1
Simas
  • 43,548
  • 10
  • 88
  • 116