title: Timer running itself without buttons?
body: <p>I have found an activity with two buttons that starts and stops a timer but can't make it work 
  without buttons,I want it to start itself oncreate. I tried to put a runnable like this but it didn't work  </p>
  
  <pre><code>class Starter implements Runnable {
              public void run() {
                  mCount= 0;
                  timerStart();
  
              }
  
      }
  </code></pre>
  
  <p>here is my main activity </p>
  
  <pre><code>import java.util.Timer;
  import java.util.TimerTask;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  
  
  public class MainActivity extends Activity {
  
        TextView mTextView;
          Button   mButtonStart;
          Button   mButtonStop;
  
          int      mCount;
          Timer    mTimer;
  
          Handler  mHandler= new Handler();
  
          Runnable Time= new Runnable() {
              @Override 
              public void run() {
                  mCount++; 
                  mTextView.setText("Count="+mCount);
              }
          };
  
          protected class TheTimerTask extends TimerTask {
              @Override public void run() {
                  // What we want to say is
                  //  mCount++; 
                  //  mTextView.setText("Count="+mCount);
                  // But this gives "Only the original thread that created a view hierarchy can touch its views."
                  mHandler.post(Time);
              }
          }
  
  
  
  
  
  
          protected void timerStart() {
              if( mTimer==null ) {
                  mTimer = new Timer();
                  mTimer.schedule( new TheTimerTask(), 0, 100 ); // 100 milli seconds
              }
          }
  
          protected void timerStop() {
              if( mTimer!=null ) {
                  mTimer.cancel(); 
                  mTimer= null;
              }
          }
  
  
          OnClickListener mButtonStart_OnClick = new OnClickListener() {
              @Override public void onClick(View v) {
                  mCount= 0;
                  timerStart();
              }
          };
  
          OnClickListener mButtonStop_OnClick = new OnClickListener() {
              @Override public void onClick(View v) {
                  timerStop();
              }
          };
  
  
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
  
              mTextView= (TextView)findViewById(R.id.textview);
              mButtonStart= (Button)findViewById(R.id.buttonstart);
              mButtonStop= (Button)findViewById(R.id.buttonstop);
  
              mButtonStart.setOnClickListener(mButtonStart_OnClick);
              mButtonStop.setOnClickListener(mButtonStop_OnClick);
  
          }
      }
  </code></pre>
  
  <p>and my xml:</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:id="@+id/textview"
       android:text="Press start to start timer"/&gt;
  
  &lt;Button android:layout_width="wrap_content"
       android:layout_height="wrap_content" 
       android:id="@+id/buttonstart" 
       android:text="Start"&gt;&lt;/Button&gt;
  
  &lt;Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
       android:text="Stop"
        android:id="@+id/buttonstop"&gt;&lt;/Button&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>