Skip to content

Instantly share code, notes, and snippets.

@derekstavis
Created October 15, 2012 18:43

Revisions

  1. derekstavis revised this gist Oct 15, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion DigitalClock.java
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ public DigitalClock(Context context) {
    private void update() {
    seconds++;

    if (seconds > 60) {
    if (seconds >= 60) {
    seconds = 0;
    if (minutes < 59) {
    minutes++;
  2. derekstavis revised this gist Oct 15, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion DigitalClock.java
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ private void update() {

    if (seconds > 60) {
    seconds = 0;
    if (minutes < 60) {
    if (minutes < 59) {
    minutes++;
    } else if (hours < 23) {
    minutes = 0;
  3. derekstavis revised this gist Oct 15, 2012. No changes.
  4. derekstavis revised this gist Oct 15, 2012. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions DigitalClock.java
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,6 @@ public class DigitalClock extends TextView {

    private Timer clockTimer;
    private final TimerTask clockTask = new TimerTask() {

    @Override
    public void run() {
    mHandler.post(mUpdateResults);
    @@ -16,11 +15,10 @@ public void run() {

    final Handler mHandler = new Handler();
    final Runnable mUpdateResults = new Runnable() {
    public void run() {

    update();
    }
    };
    public void run() {
    update();
    }
    };

    public DigitalClock(Context context) {
    super(context);
  5. @invalid-email-address Anonymous created this gist Oct 15, 2012.
    73 changes: 73 additions & 0 deletions DigitalClock.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    package com.derekstavis;

    public class DigitalClock extends TextView {
    private int hours;
    private int minutes;
    private int seconds;

    private Timer clockTimer;
    private final TimerTask clockTask = new TimerTask() {

    @Override
    public void run() {
    mHandler.post(mUpdateResults);
    }
    };

    final Handler mHandler = new Handler();
    final Runnable mUpdateResults = new Runnable() {
    public void run() {

    update();
    }
    };

    public DigitalClock(Context context) {
    super(context);
    init();
    }

    private void update() {
    seconds++;

    if (seconds > 60) {
    seconds = 0;
    if (minutes < 60) {
    minutes++;
    } else if (hours < 23) {
    minutes = 0;
    hours++;
    } else {
    minutes = 0;
    hours = 0;
    }
    }
    if (seconds % 2 == 0) {
    setText(String.format("%02d:%02d", hours, minutes));
    } else {
    setText(String.format("%02d %02d", hours, minutes));
    }
    }

    private void init() {
    clockTimer = new Timer();

    Calendar mCalendar = Calendar.getInstance();
    hours = mCalendar.get(Calendar.HOUR_OF_DAY);
    minutes = mCalendar.get(Calendar.MINUTE);
    seconds = mCalendar.get(Calendar.SECOND);

    clockTimer.scheduleAtFixedRate(clockTask, 0, 1000);
    }

    public DigitalClock(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    }

    public DigitalClock(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
    }

    }
    5 changes: 5 additions & 0 deletions your_layout.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    <com.derekstavis.DigitalClock
    android:id="@+id/digitalClock"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="120dp"/>