hockeyfreak863
hockeyfreak863

Reputation: 59

Update a TextView in android with for loop and 1000 millisecond delay

I cannot make a simple program to update the UI.

What I am looking to do is create a timer in a for loop that is initiated on a button click

public void buttonClick(View v);

for(int i=0;i<100;i++){
    textView.setText(i+"");

    try{
        thread.sleep(1000);
        catch{(yadayadayada)
    }
}

I'm trying to make a counter in a for loop to update a text view with a one millisecond delay.

My second try and still no good

package com.example.dbq;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    Handler mHandler = new Handler();
    TextView tv1;
    Button b1;
    int n=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv1=(TextView)findViewById(R.id.textView1);
        b1 =(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i<100;i++){ 
                    Runnable r=new Runnable() {
                        @Override
                        public void run() {
                            tv1.setText(n +" runs");
                        }
                    };

                    mHandler.post(r);
                    mHandler.postDelayed(r, 1000);
                    n++;
                }
            }
        }); //end onclick
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Upvotes: 0

Views: 2089

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

Use a timer scheduled at a fixed rate. The below updated text view with a count which is incremented every second.

Remember to update text view on the UI thread and cancel the timer whenever required.

    _tv = (TextView) findViewById( R.id.textView1 );

On Button click initiate a timer. Start button to start a timer.

    _t = new Timer(); 
    _t.scheduleAtFixedRate( new TimerTask() {
            @Override
            public void run() {
                _count++;

                runOnUiThread(new Runnable() //run on ui thread
                 {
                  public void run() 
                  { 
                     _tv.setText(""+_count);
                 }
                 });
            }
        }, 1000, 1000 ); 

Have another button stop to cancel the timer

        _t.cancel();

You can also use a handler as suggested by WebnetMobile

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

Do not use thread.sleep - you do not need to sleep your thread - you need update your UI at certain intervals and this is something different. Simply use Handler, then create Runnable that either updates your UI or calls the methods to do so and finally post your Runnable using Handler.postDelayed(). To keep the timer work, your Runnable have to post itself again, but that's shall be pretty obvious. And remember to remove your queued runnable when you are going to leave your fragment/activity

Upvotes: 4

Related Questions