Skip to content

Instantly share code, notes, and snippets.

@samwize
Last active December 25, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samwize/6903896 to your computer and use it in GitHub Desktop.
Save samwize/6903896 to your computer and use it in GitHub Desktop.
Add custom font to timerTextView
package com.example.stopwatch;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
// The system time when timer starts
private long startTime;
// The total elapsed time in milliseconds
private long elapsedTime;
// Keep track of timer status
private boolean hasStarted = false;
// OS handler
private Handler mHandler = new Handler();
// The views
private TextView timerTextView;
private Button startStopButton;
private Button resetButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Maps the view to our private variables
timerTextView = (TextView)findViewById(R.id.timerTextView);
startStopButton = (Button)findViewById(R.id.startStopButton);
resetButton = (Button)findViewById(R.id.resetButton);
// Set custom font
Typeface font = Typeface.createFromAsset(getAssets(), "CutiveMono.ttf");
timerTextView.setTypeface(font);
}
// Method to return the timer format from a time (in millisecond) to eg. 52:32.5
private String convertToTimerFormat(float time) {
// Get the minutes
long min = (long)((time/1000)/60);
String minString = String.valueOf(min);
if(min <= 0)
minString = "00";
else if(min < 10)
minString = "0" + minString;
// Get the trailing seconds
long sec = (long)(time/1000);
sec = sec % 60;
String secString = String.valueOf(sec);
if(sec <= 0)
secString = "00";
else if(sec < 10)
secString = "0" + secString;
// Get the tenth-of-a-second
long tenthSec = (long)(time/100);
tenthSec = tenthSec % 10;
String tenthSecString = String.valueOf(tenthSec);
return minString + ":" + secString + "." + tenthSecString;
}
// Create a Runnable that when runs, update the timer string every tenth of a second
private Runnable startTimer = new Runnable() {
@Override
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
String timerString = convertToTimerFormat(elapsedTime);
timerTextView.setText(timerString);
// We loop every tenth of a second
mHandler.postDelayed(this, 100);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void startStopClick(View view) {
if (hasStarted) {
// Timer was in start state, so now stop it
mHandler.removeCallbacks(startTimer);
// Update button text
startStopButton.setText("Start");
} else {
// Timer was in stop state, so now start it
startTime = System.currentTimeMillis() - elapsedTime;
mHandler.removeCallbacks(startTimer);
mHandler.postDelayed(startTimer, 0);
// Update button text
startStopButton.setText("Stop");
}
// Toggle start and stop state and update button text
hasStarted = !hasStarted;
}
public void resetClick(View view) {
// Reset the variables
startTime = System.currentTimeMillis();
elapsedTime = 0;
timerTextView.setText("00:00.0");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment