Android handler periodically

This is what I want to achieve:

  • Activity starts without ClickListener and has four text images with a white background

  • I want to change the color of text 1 to blue. Wait 2 seconds, then change it to white and change text 2 to blue. wait 2 seconds and then change it to white ... so until I converted text 4 to blue and back to white.

  • Once this is complete, I want to add a ClickListener and wait for user input.

How can i achieve this? I am new to Android, but understand the details.

+6
android handler
source share
3 answers

There is no need to create a stream for this or animation.

The solution is very 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 reliable implementation, be sure to delete any pending messages at least by Activity.onDestroy (). (Or, if you publish them in Activity.onStart (), delete them in Activity.onStop (), if you send them to Activity.onResume (), delete them in Activity.onPause ().)

+1
source share

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

Life Life has a good tutorial to get you started with the animation . Jeff has a series of training sections in two parts - part 1 , part 2 .

Hope this helps, indyfromoz

+2
source share

I have one sample for this task, but through thread with handleMessage

import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.widget.EditText; import android.widget.TextView; public class l15_threadOneaacto extends Activity { /** Called when the activity is first created. */ TextView tv[]=new TextView[4]; EditText et; Thread bcko; static int index=0; boolean isRunning=false; boolean acceptevent=false; Handler hn=new Handler(){ @Override public void handleMessage(android.os.Message msg) { switch (index) { case 0: tv[0].setBackgroundColor(Color.BLUE); break; case 1: tv[0].setBackgroundColor(Color.WHITE); break; case 2: tv[1].setBackgroundColor(Color.BLUE); break; case 3: tv[1].setBackgroundColor(Color.WHITE); break; case 4: tv[2].setBackgroundColor(Color.BLUE); break; case 5: tv[2].setBackgroundColor(Color.WHITE); break; case 6: tv[3].setBackgroundColor(Color.BLUE); break; case 7: tv[3].setBackgroundColor(Color.WHITE); break; } index++; if(index==8){ acceptevent=true; et=(EditText)findViewById(R.id.bbb); et.setText("ready for input"); } }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv[0]=(TextView)findViewById(R.id.tx1); tv[1]=(TextView)findViewById(R.id.tx2); tv[2]=(TextView)findViewById(R.id.tx3); tv[3]=(TextView)findViewById(R.id.tx4); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); bcko=new Thread (new Runnable() { @Override public void run() { try { // for(int i=0;i<8 && isRunning;i++){ Thread.sleep(2000); hn.sendMessage(hn.obtainMessage()); } } catch (Exception e) { // TODO: handle exception } } }); isRunning=true; bcko.start(); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); isRunning=false; } } 

kayout:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tx1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFFFF" android:text="" /> <TextView android:id="@+id/tx2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFFFF" android:text="" /> <TextView android:id="@+id/tx3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFFFF" android:text="" /> <TextView android:id="@+id/tx4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFFFF" android:text="" /> <EditText android:id="@+id/bbb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="not ready " /> </LinearLayout> 

Good luck

0
source share

All Articles