Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Handler Periodically

This I want I want to achieve:

  1. An activity starts with no ClickListener and has four textviews all with white background

  2. I want to change the color of textview 1 to blue. Wait for 2 seconds and then change it back to white and change the textview 2 to blue. wait for 2 seconds and then change it back to white... so on till i have turned textview 4 to blue and back to white.

  3. Once that is complete, I want to add the ClickListener and wait for user input.

How can I achieve this? I am new to Android but understands bits and pieces.

like image 825
SingleMalt Avatar asked Dec 04 '10 21:12

SingleMalt


People also ask

When to use Handler in android?

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed at some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

How can you perform repeated tasks in a service in Android?

There are at least four ways to run periodic tasks: Handler - Execute a Runnable task on the UIThread after an optional delay. ScheduledThreadPoolExecutor - Execute periodic tasks with a background thread pool. AlarmManager - Execute any periodic task in the background as a service.

What is Handler in java?

In android Handler is mainly used to update the main thread from background thread or other than main thread. There are two methods are in handler. Post() − it going to post message from background thread to main thread using looper.

How do you make kotlin handler?

1. Run handler in main thread. Looper includes a helper function, getMainLooper() , which retrieves the Looper of the main thread. You can run code in the main thread by using this Looper to create a Handler .


2 Answers

You can achieve this by creating Animation sequences, in either XML or Java code, and triggering them in sequence. You will need to define a animation sequence with LayoutAnimationController, at the end of the animation, you can add the ClickListener.

Developer Life has a good tutorial to get you started on animations. Jeff has a two-part tutorial series on animations - part 1, part 2.

Hope this helps, indyfromoz

like image 128
indyfromoz Avatar answered Sep 20 '22 13:09

indyfromoz


There is no need to create a thread for this, or animations.

The solution is really simple: use Handler.postDelayed() or Handler.sendMessageDelayed():

http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long) http://developer.android.com/reference/android/os/Handler.html#sendMessageDelayed(android.os.Message, long)

For a robust implementation, be sure to remove any pending messages at least by Activity.onDestroy(). (Or if you are posting them in Activity.onStart(), remove them in Activity.onStop(); if posting in Activity.onResume(), remove in Activity.onPause().)

like image 25
hackbod Avatar answered Sep 18 '22 13:09

hackbod