simone
simone

Reputation: 3

How to set countdown timer with only second and decimal?

How can i set a countdown timer with only second and decimal?
Ex: 39 (seconds), 9(decimal)

this is my code at the moment!

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   final Button timer = findViewById(R.id.countdown);

    timer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Timer for the text

            new CountDownTimer(40000, 1000) {

                public void onTick(long millisUntilFinished) {
                    final long decimal;
                    decimal = ?? // help me here 
                    timer.setText("" + millisUntilFinished / 1000+ "." + decimal);
                }

                public void onFinish() {
                    timer.postDelayed(new Runnable() {
                        public void run() {
                        }
                    }, 10000);

                }

            }.start();

        }
    }); // end timer
}

Thank you very much for the help!

Upvotes: 0

Views: 241

Answers (2)

Brian Baldovino
Brian Baldovino

Reputation: 26

new CountDownTimer(40000, 1) {

                public void onTick(long millisUntilFinished) {
                    timer.setText("" + millisUntilFinished / 1000 + ":" + (millisUntilFinished % 1000) / 10 + " sec");
                    timer.setClickable(false);
                }

                public void onFinish() {
                    timer.postDelayed(new Runnable() {
                        public void run() {
                        }
                    }, 1000);
                    timer.setText("Start again");
                    timer.setClickable(true);
                }

            }.start();

        }
    }); // end timer

This should work, good job!

Upvotes: 1

Shalauddin Ahamad Shuza
Shalauddin Ahamad Shuza

Reputation: 3657

You can use RxJava timer operator. Create timer observable

Observable.timer(39, TimeUnit.SECONDS)

It will give event after 39 seconds. Details example is here.

Upvotes: 0

Related Questions