Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is your layout</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="0" android:gravity="center_horizontal" android:layout_marginBottom="40dp" android:layout_marginTop="20dp" android:textSize="27dp" android:id="@+id/txtView"/&gt; &lt;RelativeLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="center"&gt; &lt;Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" android:id="@+id/startButton"/&gt; &lt;Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" android:id="@+id/stopButton" android:layout_toRightOf="@id/startButton"/&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p><br/> Here is your activity class</p> <pre><code>import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MyActivity extends Activity implements View.OnClickListener { TextView txt = null; Button startButton = null; Button stopButton = null; CountDownTimerWithPause timer = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txt = (TextView) findViewById(R.id.txtView); startButton = (Button) findViewById(R.id.startButton); stopButton = (Button) findViewById(R.id.stopButton); startButton.setOnClickListener(this); stopButton.setOnClickListener(this); } @Override public void onClick(View view) { if (view == startButton) { if (timer == null) { initTimer(); } timer.resume(); } if (view == stopButton) { timer.pause(); } } private void initTimer() { timer = new CountDownTimerWithPause(10000, 1000, false) { @Override public void onTick(long millisUntilFinished) { Integer currentValue = Integer.valueOf((String) MyActivity.this.txt.getText()); MyActivity.this.txt.setText(String.valueOf(currentValue + 1)); } @Override public void onFinish() { Toast.makeText(MyActivity.this, "finish", Toast.LENGTH_SHORT).show(); } }; timer.create(); } } </code></pre> <p>Do not forget to copy <a href="http://www.java2s.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm" rel="nofollow">CountDownTimerWithPause</a> into your package.</p> <p>Works for me, <strong>BUT</strong>...</p> <p>I mention some bugs but it depends on your application. Maybe it's OK for you. Bug is - when starting timer, it fires immediatelly. 'So what?' you might ask. Imagine you click Stop button when 3 and half seconds passed. So when you click Start button you expect half second pass before you see 4, but not in that class implementation.</p> <p>And there is some strange delay at the end of counting. I'd recommend to search for better implementation.</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