Creating Timer in Android
Hello guys, in this tutorial we are going to see how to create a timer and to do something when the timer reaches a particular time. So lets do it.
activity_main.xml
MainActivity.java
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<TextView
android:id="@+id/timerValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/pauseButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="37dp"
android:textSize="40sp"
android:textColor="#ffffff"
android:text="00:00:000" />
<Button
android:id="@+id/startButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="38dp"
android:text="start" />
<Button
android:id="@+id/pauseButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignBaseline="@+id/startButton"
android:layout_alignBottom="@+id/startButton"
android:layout_alignParentRight="true"
android:layout_marginRight="38dp"
android:text="pause" />
</RelativeLayout>
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button start, pause;
TextView time;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
boolean stopTimer = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.startButton);
pause = (Button) findViewById(R.id.pauseButton);
time = (TextView) findViewById(R.id.timerValue);
start.setOnClickListener(this);
pause.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.startButton:
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
break;
case R.id.pauseButton:
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
break;
}
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
String localtime = "" + mins + ":" + String.format("%02d", secs)
+ ":" + String.format("%03d", milliseconds);
time.setText(localtime);
if (mins == 1) {
stopTimer = true;
Toast.makeText(MainActivity.this, "You guys are awesome",
Toast.LENGTH_SHORT).show();
}
if (!stopTimer)
customHandler.postDelayed(this, 0);
}
};
}
Comments
Post a Comment