tags:

views:

184

answers:

3

Hi all,in my application i want to change button text for every 3 sec.

+2  A: 

Hi,

Set up a Timer to run a TimerTask every 3 seconds. The TimerTask only has to call the button's setText method to change the button's text. You would have to do this within the UI thread, so you should use post to run a Runnable object that will perform the update an the correct thread.

For example, in the following activity, the letter "A" is added to the button's text every three seconds:

public class ButtonTest extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button subject = new Button(this);
        subject.setLayoutParams((new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT)));
        subject.setText("A");
        setContentView(subject);

        Timer timing = new Timer();
        timing.schedule(new Updater(subject), 3000, 3000);
    }

    private static class Updater extends TimerTask {
        private final Button subject;

        public Updater(Button subject) {
            this.subject = subject;
        }

        @Override
        public void run() {
            subject.post(new Runnable() {

                public void run() {
                    subject.setText(subject.getText() + "A");
                }
            });
        }
    }
}
Klarth
But you won't be able to update UI thread without a Handler.
Nikola Smiljanić
well you derive from TimerTask anyway so pass a reference in your own function
tm1rbrt
hi,please provide some sample code for Timmer
deepthi
hi, please see my updated post with a complete example
Klarth
thank you very much it's working
deepthi
A: 

Using AsyncTask you can do something like this to update a Button named button1

private class UpdateButton extends AsyncTask<Void, String, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        int i = 0;
        while ( true ) {
            publishProgress("" + i++);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                return (Void)null;
            }
        }
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);

        button1.setText(values[0]);
    }       
}

to start button1 update

(new UpdateButton()).execute((Void)null);
dtmilano
A: 

Have a look at this neat little example: http://developer.android.com/resources/articles/timed-ui-updates.html

All you need is a handler and a task! Within the Task do your actions (setting the text) and set up the next handler event using Handler.postDelayed.

But keep the following in mind: I would strongly advise you to set up the first timer event in the activity's onResume() method and NOT in its onCreate method. Also, remove (cancel) your handler in onPause()!

To understand the meaning of this, have a look at an activity's lifecycle.

That way you prevent to have anything happening while your activity is in the background and not visible!

Zordid
Thank you very much
deepthi