views:

21

answers:

1

Hey. I just started with Android. I'm using the ViewFlipper layout with 2 LinearLayouts. First layout has a button that switches to the second layout. I would like to add a timer that would switch to the first layout after 3000ms. I tried with a Thread but it did not work (can not communicate with UI element of other thread).

My code:

public class Test extends Activity {

ViewFlipper f;
LinearLayout l1;
LinearLayout l2;
Button b1;

Thread s;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    f = (ViewFlipper) findViewById(R.id.f);
    l1 = (LinearLayout) findViewById(R.id.l1);
    l2 = (LinearLayout) findViewById(R.id.l2);
    b1 = (Button) findViewById(R.id.b1);

    updateSwitch = new Thread() {
        @Override
        public void run() {
            try {sleep(3000);
            } catch (InterruptedException e) {
            } finally {f.showPrevious();}
        }
    };

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            f.setAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.push_left_in));
            lFlipper.showNext();
            updateSwitch.start();
        }
    });
}

}

My guess is that I need some kind of handler that would connect a new Thread with the main thread. Please update my code. Thx 10x.

+2  A: 

You need to read a technical article about Updating the UI from a Timer

Pentium10