Tikitaka
Tikitaka

Reputation: 460

Set delay in android

I want to show 1 to 9 numbers in my app as a counter


TextView te=(TextView)findViewById(R.id.text);

for(int i=0;i<10;i++ ){ try { te.setText("Number"+i); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }

But this doesn't give what i want How can i do this
Thank you

Upvotes: 3

Views: 7926

Answers (4)

Arpan Sharma
Arpan Sharma

Reputation: 2162

You can use CountDownTimer directly. this link might help you http://developer.android.com/reference/android/os/CountDownTimer.html

you can display the required values in the ontick method

Upvotes: 0

Chintan Soni
Chintan Soni

Reputation: 25267

You can try code similar to this:

import android.os.Bundle;
import android.view.MotionEvent;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;

public class SplashScreen extends Activity {

    protected int _splashTime = 5000;
    private Thread splashTread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    startActivity(new Intent(SplashScreen.this,
                            Home.class));
                    finish();
                }
            }
        };
        splashTread.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            synchronized(splashTread)
            {
                    splashTread.notifyAll();
            }
        }
        return true;
    }
}

Make the necessary changes to get your thing work perfectly. This is just an example.

Upvotes: 1

pierrotlefou
pierrotlefou

Reputation: 40721

If no user interaction was expected when displaying the counter ,what you did should be fine ,just making sure they are running on the UI thread, which could be achieved by putting it in runOnUiThread or creating an Hander and then postMessage.

If user interaction was expected during the counter increasing,you can create a Handler and then call postDelayed.

Upvotes: 0

waqaslam
waqaslam

Reputation: 68177

Never ever put delay on UI thread. Instead, use Handler and its postDelayed method to perform operations at a regular interval. For example:

//define 'int i = 10;' at global level
final Handler handler = new Handler();
handler.post(new Runnable() {
    @Override
    public void run() {
        te.setText("Number"+i);
        i--;
        handler.postDelayed(this, 1000);
    }
});

Upvotes: 10

Related Questions