Adil Bhatty
Adil Bhatty

Reputation: 17340

Counter in android

I want to create stop watch kind of counter in android. Having a button whichi I press the counter starts and when I press the stop button it stops.

I have written this code but it doesn't seem to work

public class counter extends Activity {
/** Called when the activity is first created. */
Button btnStart;
TextView txtCounter;
boolean status = false;
int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btnStart = (Button) findViewById(R.id.btnStart);
    txtCounter = (TextView) findViewById(R.id.txtCounter);
    initCouner();
   btnStart.setOnClickListener(new OnClickListener() {  
    @Override
    public void onClick(View v) {
        if(status == false)
        {               
            for(int i=0; i < 500; i++)
            {
            txtCounter.setText(String.valueOf(i));
            }
            btnStart.setText("Stop");
            status = true;
        }
        else if (status == true)
        {

            btnStart.setText("Start");
            status = false;
            initCouner();
        }
    }
});

}
public void initCouner()
{
    btnStart.setText("Start");
    txtCounter.setText("0");
}}

When ever the loop starts it shows 499, not showing the counting. and also the stop button not working.

Thanks

Upvotes: 0

Views: 4946

Answers (4)

Adil Bhatty
Adil Bhatty

Reputation: 17340

With the help of T0X1C's ans, I came up with the following code:

public void onClick(View v) {
            if(status == false)
            {       
                btnStart.setText("Stop");
                status = true;


                new Thread(new Runnable() {


                    @Override
                    public void run() {
                        // TODO Auto-generated method stub


                        while(status!=false)
                        {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub

                                    txtCounter.setText(String.valueOf(counter));
                                    counter++;
                                }
                            });
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    }



                }).start();

            }

    else if (status == true)
            {

                btnStart.setText("Start");
                status = false;
                initCouner();
            }
        }
    });

Upvotes: 2

ingsaurabh
ingsaurabh

Reputation: 15269

Change your code to this

public void onClick(View v) {
    if(status == false)
    {       
        btnStart.setText("Stop");
        status = true;        
        new Thread(new Runnable()
        { run(){
              for(int i=0; i < 500; i++)
              {
                  runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                txtCounter.setText(String.valueOf(i));
                            }
                        });
                   Thread.sleep(1000);
              }}
        }).start();

    }
    else if (status == true)
    {

        btnStart.setText("Start");
        status = false;
        initCouner();
    }
}
});

Upvotes: 1

denis.solonenko
denis.solonenko

Reputation: 11775

I suggest you to take a look at this article http://developer.android.com/resources/articles/timed-ui-updates.html Your current approach is not going to work..

Upvotes: 0

Matthieu
Matthieu

Reputation: 16397

Maybe the problem is that you should be doing the counting in a separate thread. Look into asynctask ( http://developer.android.com/reference/android/os/AsyncTask.html ) to do that.

Upvotes: 0

Related Questions