Monday, May 20, 2013

Customizing the Display Time of Toast

By default a toast is displayed for 1 or 2 seconds. We can customize this thime and can show the toast for longer time 5,10,20 seconds etc.

We use  CountDownTimer class for this purpose.

CountDownTimer  

IT is an abstarct class you can see details  CountDownTimer



Display  Toast  for longer time

In this example we will show the toast for 20Seconds (20000 milliseconds)


public class ToastActivity extends Activity
{
    AlertDialog dialog;
   
     static CountDownTimer timer =null;
     Toast toast;
     @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                

                // creating toast and setting properties

                toast = new Toast(this);
                TextView textView=new TextView(this);
                textView.setTextColor(Color.BLUE);
                textView.setBackgroundColor(Color.TRANSPARENT);
                textView.setTextSize(20);
                textView.setText("This Toast will Display for 20 Seconds in Center of The Screen");
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);


                toast.setView(textView);
               

               //    Toast Display tTime Settings

               
                // Create the CountDownTimer object and implement the 2 methods
                // show the toast in onTick() method  and cancel the toast in onFinish() method
                // it will show the toast for 20 seconds (20000 milliseconds 1st argument) with interval of 1 second(2nd argument)

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

                }.start();
               
          }
}

 

We can also cancel our toast before specified tine of 20 Seconds  on a particular condition with following code


// Cancelling toast on a particular condition

                if(your conditinal expression)
                {
                    timer.cancel();
                }




 

Advance Android Topics


                   Customizing Toast In Android 
                   Showing Toast for Longer Time
                   Customizing the Display Time of Toast
                   Using TimePickerDialog and DatePickerDialog In android
                   Animating A Button In Android
                    Populating ListView With DataBase

                    Customizing Checkboxes In Android 
                    Increasin Size of Checkboxes
                    Android ProgressBar
                    Designing For Different Screen Sizes
                    Handling Keyboard Events

More Android Topics:



  

Android : Introduction


       Eclipse Setup for Android Development

                     Configuring Eclipse for Android Development

          Begging With Android

                     Creating Your First Android Project
                     Understanding Android Manifest File of your android app


         Working With Layouts

                      Understanding Layouts in Android
                          Working with Linear Layout (With Example)
                                Nested Linear Layout (With Example)
                          Table Layout
                          Frame Layout(With Example)
                         Absolute Layout
                         Grid Layout


       Activity

                     Activity In Android
                     Activity Life Cycle
                     Starting Activity For Result
                     Sending Data from One Activity to Other in Android
                     Returning Result from Activity

     Working With Views

                     Using Buttons and EditText in Android 
                     Using CheckBoxes in Android 
                     Using AutoCompleteTextView in Android
                     Grid View

       Toast

                     Customizing Toast In Android
                     Customizing the Display Time of Toast
                     Customizing Toast At Runtime
                     Adding Image in Toast
                     Showing Toast for Longer Time

     Dialogs In Android

                     Working With Alert Dialog
                     Adding Radio Buttons In Dialog
                     Adding Check Boxes In Dialog
                     Creating Customized Dialogs in Android
                    Adding EditText in Dialog

                   Creating Dialog To Collect User Input

                 DatePicker and TimePickerDialog

                              Using TimePickerDialog and DatePickerDialog In android

    Working With SMS

                  How to Send SMS in Android
                  How To Receive SMS
                  Accessing Inbox In Android

    ListView:

               Populating ListView With DataBase

      Menus In Android

                    Creating Option Menu
                    Creating Context Menu In Android

      TelephonyManager

                    Using Telephony Manager In Android

     Working With Incoming Calls

                    How To Handle Incoming Calls in Android
                    How to Forward an Incoming Call In Android
                   CALL States In Android
 

    Miscellaneous

                   Notifications In Android
                   How To Vibrate The Android Phone
                   Sending Email In Android
                  Opening a webpage In Browser
                   How to Access PhoneBook In Android
                   Prompt User Input with an AlertDialog

   Storage:  Storing Data In Android


               Shared Prefferences  In Android

                             SharedPreferences In Android

               Files: File Handling In Android

                              Reading and Writing files to Internal Stoarage
                              Reading and Writing files to SD Card 
                           

                DataBase : Working With Database

                             Working With Database in Android
                             Creating Table In Android
                             Inserting, Deleting and Updating Records In Table in Android
                             How to Create DataBase in Android
                             Accessing Inbox In Android

     Animation In Android:

                  Animating A Button In Android





4 comments:

  1. Where do you place the


    if(your conditinal expression)
    {
    timer.cancel();
    } ?


    I am new to Android programming.

    ReplyDelete
  2. Thank you!!!! This saved me a TON of extra work.

    ReplyDelete
  3. First, CountDownTimer is not really an abstract class. If it were, you would not be able to instantiate it with new, which you do in the code.
    Second, you need to show the toast you constructed before the timer is activated. Otherwise, the toast will be shown with approx. a 1-second delay, upon the first onTick() call.
    Finally, notice that if the duration is set to 5000 msec, you will only receive 3 onTick() callbacks, with just below 4, 3 and 2 seconds to finish (to prove this, insert a Log.d() statement into onTick() to print the remaining time). This means that the Toast has to sustain at least 2 seconds of showing by itself, which can be a problem with a default short duration setting. So, to be on the safe side, set the onTick interval to 500 not 1000 msec.

    ReplyDelete