Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you can use the <code>Timer</code> class to start a timer session. Follow the steps:</p> <p>1- define a global variable of the <code>Timer</code> and a variable to count the time like:</p> <pre><code>private Timer t; private int TimeCounter = 0; </code></pre> <p>2- then when the activity starts, so in <code>onCreate</code> add the following: <strong>P.S: what I did is I have a textView to show the timing while he is writing the sentence. so if you dont want that you can delete the <code>tvTimer</code> part in the following code</strong></p> <pre><code>t = new Timer(); t.scheduleAtFixedRate(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub runOnUiThread(new Runnable() { public void run() { tvTimer.setText(String.valueOf(TimeCounter)); // you can set it to a textView to show it to the user to see the time passing while he is writing. TimeCounter++; } }); } }, 1000, 1000); // 1000 means start from 1 sec, and the second 1000 is do the loop each 1 sec. </code></pre> <p>then when the button is clicked, stop the timing and show the <code>timeCounter</code> varaible in the <code>Toast</code>.</p> <pre><code>t.cancel();//stopping the timer when ready to stop. Toast.makeText(this, "The time taken is "+ String.valueOf(TimeCounter), Toast.LENGTH_LONG).show(); </code></pre> <p><strong>P.S: You have to deal with converting the seconds into minutes because the number could extends to 360 secs so you need to convert it to 6 minutes. you can do it in the <code>t.schedualeAtFixedRate</code> or after you done you can convert it and show it in the toast</strong></p> <p>hope you found this useful. please give me a feedback if it worked for you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload