Heretic Monk
Heretic Monk

Reputation: 397

Updating a textView after certain time elapsed

I am using System.NanoTime to track elapsed time, however it does not update on the UI. Following is my code:

I am however doing everything in the onCreate() method, the approach I've taken might not be robust, but that's where I want more ideas.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    long startTime = System.nanoTime();
    // ... the code being measured ...
    long estimatedTime = (System.nanoTime() - startTime) / 1000000000;
    System.out.println(""+estimatedTime);
    while (estimatedTime <= 100){
        System.out.println(""+estimatedTime);

        if(estimatedTime == 1){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Preparing"); 

        }
        if(estimatedTime == 2){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Inatializing"); 

        }
        if(estimatedTime == 3){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Preparing to install"); 

        }
        if(estimatedTime == 4){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Installing"); 

        }
        if(estimatedTime == 5){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Installed"); 

        }
        if(estimatedTime == 6){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Unzipping packages..."); 

        }


        estimatedTime = (System.nanoTime() - startTime) / 1000000000;
    }

}

Upvotes: 1

Views: 1088

Answers (4)

Kailash Ahirwar
Kailash Ahirwar

Reputation: 769

Better to use Handler to delay some task...

Handler handler = new Handler();
handler.postDelayed(new Runnable(){ 
    public void Run(){
        //Put your code here. Code will execute after 1000ms
    }
}, 1000);

Upvotes: 1

Nitin
Nitin

Reputation: 1986

Use another thread and call runOnUiThread method and pass the object of that thread.like following

Thread th=new Thread(new Runnable() {



        @Override
        public void run() {
            //put your code here
while (estimatedTime <= 100){
Thread.sleep(1000);
            System.out.println(""+estimatedTime);

        if(estimatedTime == 1){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Preparing"); 

        }
        if(estimatedTime == 2){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Inatializing"); 

        }
        if(estimatedTime == 3){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Preparing to install"); 

        }
        if(estimatedTime == 4){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Installing"); 

        }
        if(estimatedTime == 5){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Installed"); 

        }
        if(estimatedTime == 6){
            TextView tx = (TextView)findViewById(R.id.textView);
            tx.setText("Unzipping packages..."); 

        }


        estimatedTime = (System.nanoTime() - startTime) / 1000000000;
        }
    });

runOnUiThread(th);

Upvotes: 1

Dariusz Mazur
Dariusz Mazur

Reputation: 587

Maybe you can try:

tx.postInvalidate();

where tx is your TextView object

after this line:

estimatedTime = (System.nanoTime() - startTime) / 1000000000;

But you should all this code which is below "while" put into Thread. Example:

new Thread(new Runnable() {

        @Override
        public void run() {
            //your code here
        }
    }).start();

UPDATE: Update main thread you can using this:

YourActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //here set text into textView
    }
});

Upvotes: 3

danijoo
danijoo

Reputation: 2843

You cant do that in the UI thread as its blocking. You should move that to an AsyncTask.

Upvotes: 1

Related Questions