Mahesh Miragi
Mahesh Miragi

Reputation: 1

How to implement delay in android?

I want to print sequential numbers every 2 seconds when I press button. I used following code:

int j=0;
button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            c=Calendar.getInstance();
            Delay(2 ,c.get(Calendar.SECOND));
            if(j++<5)
              t.setText("number "+j);

            }

    });

public void Delay(int p,int q){

    int z=0;

    while(z<p){
        c=Calendar.getInstance();
        i= c.get(Calendar.SECOND);
        z=i-q;
    }

    return ;
}

but this code prints "Number 5" at the end of 10 seconds directly. How can I print "Number 1", "Number 2", "Number 3"....sequentially every 2 sec.

Upvotes: 0

Views: 550

Answers (3)

Al&#233;cio Carvalho
Al&#233;cio Carvalho

Reputation: 13647

You can use the CountDownTimer for this..you just have to define the amount of time and how often you want to update. You just have to adjust the logic a bit to do print the numbers upward.

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
    new CountDownTimer(60000, 2000) {

       public void onTick(long millisUntilFinished) {
           mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
       }

       public void onFinish() {
         mTextField.setText("done!");
       }
    }.start();
}
});

Upvotes: 0

Larry Schiefer
Larry Schiefer

Reputation: 15775

Use a Runnable to post to a handler bound to the UI thread for your app rather than sleep or delay. If you sleep or delay in your onClick() method, you are blocking the UI thread and it will make your UI unresponsive.

public class MyActivity extends Activity implements Handler.Callback {
    ...
    private Handler mHandler = new Handler(this);
    private int     mNumber = 0;
    ...
    @Override
    public void onClick(View v) {
        mNumber++;
        mHandler.postDelayed(new Runnable() {
            public void run() {
                t.setText("number: " + mNumber);
            }, 2000);
    }
}

Upvotes: 0

Aleks G
Aleks G

Reputation: 57316

Note that if you're doing this on UI thread, you'll be blocking the UI thread for 10 seconds. It's much better to have a separate thread to do this:

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        new Thread() {
            public void run() {
                for(int j=1; j<=5; i++) {
                     runOnUiThread(new Runnable() {
                         @Override
                         public void run() { t.setText("number " + j); }
                    });
                    SystemClock.sleep(2000);
                }
            }
        }.start();
    }
});

This code starts a new thread (thus not blocking the UI), which iterates from 1 to 5 outputting the number (on UI thread, as it's changing the UI) and then sleeps for 2 seconds.

Upvotes: 1

Related Questions