This I want I want to achieve:
An activity starts with no ClickListener and has four textviews all with white background
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.
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.
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.
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.
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.
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 .
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
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().)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With