2

I am using a toast for Count Down Timer, so the toast should change it's text in every second. I use this to display the toast for exactly 1 second but i want the toast to repeat itself. Hope i make you understand.

 toast =    Toast.makeText(getApplicationContext(), text.getText().toString(), Toast.LENGTH_SHORT);             toast.show();
Handler handler = new Handler();
    handler.postDelayed 
               (new Runnable() {

              @Override
                 public void run() {
                            toast.cancel();
            }
        }, 1000);
  • You cannot do it. Because the minimum duration of Toast is 2 seconds. See here: http://stackoverflow.com/questions/7965135/what-is-the-duration-of-a-toast-length-long-and-length-short – Green goblin Jun 03 '14 at 05:56

4 Answers4

3

This will show a new toast every second for exactly one second.

    int count = 100; //Declare as inatance variable

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    final Toast toast = Toast.makeText(
                            getApplicationContext(), --count + "",
                            Toast.LENGTH_SHORT);
                    toast.show();
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            toast.cancel();
                        }
                    }, 1000);

                }
            });
        }
    }, 0, 1000);
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • Woow! Shivam, that worked like a charm!!! Thanks a million. Can we stop that toast from blinking anyway? –  Jun 03 '14 at 06:16
  • I don't thinking the blinking could be stopped since we are cancelling the toast and generating a new one. However, you could always use a dialog box with a just a TextView for doing a similar thing. You can keep updating the text using just the timer. No Handler required. – Shivam Verma Jun 03 '14 at 06:22
  • Oh!!!! man, there's a bug!! i have set a 1 minute timer, the textview shows `00:20` and the toast shows `00:40`, :( –  Jun 03 '14 at 06:28
  • This depends on how and when you're updating the textView content. Edited the code to display countdown timer values for 100 seconds and works fine. Also, please ensure that you cancel the timer after you've reached zero. – Shivam Verma Jun 03 '14 at 06:39
  • Still the same problem, the timer takes two seconds to update, that's why it is running half the actual timer. Anyways thnx, i'll use alert dialog (maybe). –  Jun 03 '14 at 06:52
0

run() is called after every second. so show toast there.

Handler handler = new Handler();
    handler.postDelayed 
               (new Runnable() {

              @Override
                 public void run() {
                            toast.cancel();
                            toast = Toast.makeText(getApplicationContext(), text.getText().toString(),Toast.LENGTH_SHORT);
                            toast.show();
            }
        }, 1000);
Ahmed Nawaz
  • 1,634
  • 3
  • 21
  • 51
0

This page describes a way to keep the toast be shown indefinitely. So when you have the text view of the toast on hand, you may change the text as you like.

Herman Cheung
  • 324
  • 3
  • 7
0
  1. you have to learn more about android srvices
  2. create java class extends from IntentService
  3. override this function

    @Override
    protected void onHandleIntent(Intent intent) {
    
        try {
            Toast.makeText(context,"Click on Location button to find your bus !",Toast.LENGTH_LONG).show();
    
            Thread.sleep(5000);
        } catch (InterruptedException e) {
    
            Thread.currentThread().interrupt();
        }
    }
    
  4. go to manifest an type

  5. go to your launcher java class and

    Intent intent = new Intent(this, Service_toast.class);
        startService(intent);
    
  6. ====>> for more information about services vist android devloper : https://developer.android.com/guide/components/services.html

rocambille
  • 15,398
  • 12
  • 50
  • 68
med melek
  • 1
  • 1