Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CountDownTimer in Android

I am implementing countdown timer, but its not working for me. Below is the code.

package FinalProj.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.os.CountDownTimer;

public class iFallApp extends Activity{
    public TextView textView1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //TextView textview = new TextView(this);
        //textview.setText("This is the iFall tab");
       // setContentView()
        setContentView(R.layout.ifallapp);

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

        MyCount counter = new MyCount(5000,1000);
        counter.start();


    }

    public class MyCount extends CountDownTimer{
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            }

        iFallApp app1 = new iFallApp();

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

            textView1.setText("done");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub

            textView1.setText((int) (millisUntilFinished/1000));

        }
    }

}
like image 200
tech_learner Avatar asked Dec 21 '22 14:12

tech_learner


1 Answers

It's this line causing the problem;

textView1.setText((int) (millisUntilFinished/1000));

What you do, is set a resource id for textView1, while what you are looking for is something like;

textView1.setText(Long.toString(millisUntilFinished/1000));

Also line;

        iFallApp app1 = new iFallApp();

Is rather suspicious. Remove it just in case before you end up using it accidentally. You have your iFallApp created by Android framework already, and you can pass it using thisinstead if needed.

like image 69
harism Avatar answered Jan 09 '23 19:01

harism