Thursday, 20 December 2012

Handler in Android


This Handler will hide the textview in a few seconds later.

public Handler ()

Since : API level 1

Default constructor associates this handler with the queue for the current thread. If there isn't one, this handler won't be able to receive messages.
    Define a runnable variable with your code.
      Runnable hide = new Runnable() {
 
        @Override   public void run() {
         // TODO Auto-generated method stub
        textview.setVisibility(View.INVISIBLE);
        }
     };

public final boolean postDelayed (Runnable r, long delayMillis)

Since: API Level 1

Parameters

r                     The Runnable that will be executed.
delayMillis     The delay (in milliseconds) until the Runnable will be executed.

Returns

            Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the Runnable will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.

           Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached.

Define a Handler variable
          Handler handler = new Handler();

          textview.setVisibility(View.VISIBLE);

          handler.postDelayed(hide, 4000);


After 4 seconds the textview will be INVISIBLE.


Thank you