Advertisement
lineoff

Timer

Apr 22nd, 2016
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. import android.os.CountDownTimer;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.TextView;
  7.  
  8. public class MainActivity extends AppCompatActivity {
  9.     Counter crt;
  10.     TextView tvCtr;
  11.     Button btn;
  12.     @Override
  13.     protected void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.activity_main);
  16.         btn = (Button) findViewById(R.id.start);
  17.         tvCtr =  (TextView) findViewById(R.id.tvCtr);
  18.         btn.setOnClickListener(new View.OnClickListener() {
  19.             @Override
  20.             public void onClick(View v) {
  21.                 //10000 = 10 seconds
  22.                 crt=new Counter(10001,1);
  23.                 crt.start();
  24.             }
  25.         });
  26.     }
  27.  
  28.     public class Counter extends CountDownTimer {
  29.  
  30.         Counter(long mSeconds,long range){
  31.             super(mSeconds,range);
  32.         }
  33.         @Override
  34.         public void onTick(long millisUntilFinished) {
  35.             tvCtr.setText(String.valueOf(millisUntilFinished/1000));
  36.         }
  37.  
  38.         @Override
  39.         public void onFinish() {
  40.             tvCtr.setText("Done");
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement