Text Clock – Part 1

Usually on Styling Android the focus is on individual, standalone features and techniques for Android UI / UX development. In this series we’re going to create a very simple app which will be published to Google Play. While we will certainly cover some features and techniques that we haven’t looked at before, as well as some new features in Android 4.2, we’ll also look at some of the design decisions required to get even the simplest of apps ready for market.

So what’s this app that we’re going to create? It is a simple clock widget which displays the time using words rather than numbers. The idea came from one of the custom watch faces for the Pebble, but we’ll give it an Android twist by giving it a Holo look and feel. The initial version of the app will be a simple App Widget which the user can install on their home screen:

widget

As we progress with the series, the version of Text Clock released to Google Play will be updated to match the latest published article.

The first think that we need to do is to create an Android 4.2 project named “TextClock” with the package name com.stylingandroid.textclock, and with no Activity – we’ll add one later, but we have no need for one now.

Next we’ll look at the main class that will generate the time in words:

There are a few things worth explaining here. Firstly, the methods are static, so we never need to instantiate a TimeToWords object, but it utilises a static StringBuilder member. The reason for this is to minimise the number of objects which are created when the timeToWords() method is called. There is one potential problem with this approach: If timeToWords() is called by different threads simultaneously, the use of a single StringBuilder will cause problems. Therefore we declare timeToWords() as synchronized to ensure that it can only have one active invocation. Hopefully the rest of the app design should mean that this is unnecessary, but it’s always worth considering thread safety issues, just in case things don’t get implemented in quite the way we expect at this stage.

At this point we’ll just focus on generating a 24 hour clock representation of the time, but we’ll change to optionally support a 12 hour representation later. What happens here is that we’ll generate a text string containing the time in words, and then split it into components which get returned as a String array. We use the separator “:” to indicate where we want to break. We only have three lines on the display, and in some circumstances there will be more than three distinct words (such as “twenty one fifty five”). In such cases we want to displays the hours on one line (“twenty one”), and the minutes on the other two lines (“fifty” and “five”). We also have some edge cases for 00 hours (where we want to display “midnight”), and when the time is 0 minutes past the hour (when we want to display “o’clock” instead of the minutes).

That’s the main “engine” of our app which performs all of the backend business logic. It’s pretty simple so that we can devote the remainder of this series to the UI – how we present this to the user.

In the next article we’ll start on this presentation by beginning to implement an app widget which the user can add to the home screen of their device.

The source code for this article can be found here, and TextClock is available from Google Play.

© 2013 – 2014, Mark Allison. All rights reserved. This article originally appeared on Styling Android.

Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License

Creative Commons License
Text Clock – Part 1 by Styling Android, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Terms and conditions beyond the scope of this license may be available at blog.stylingandroid.com.

Tags:

11 Responses to “Text Clock – Part 1”

  1. Tom V says:

    Hmmm, I think midnight is 0:00 exactly, and 0:30 is not midnight.

  2. p.l says:

    >There is one potential problem with this approach: If timeToWords() is called by different threads simultaneously, the use of a single StringBuilder will cause problems<
    Then why not use ThreadLocal?

    • Mark Allison says:

      You could use ThreadLocal but it could potentially lead to additional object creation. But as I mention in the article I really don’t expect this method to be called by different threads, and it’s pretty lightweight, so won’t block significantly because of the synchronized method even if it did.

      • Yuku says:

        Really? I’m using ThreadLocal in many places on my apps, to prevent slowness because of “synchronized” keyword but still preventing race problems. Does using ThreadLocal cause many unnecessary object allocations?

        • Mark Allison says:

          It depends on the code in question. But as I said, for this code it really is unnecessary and synchronized gives precisely the thread safety that I need.

  3. Android Dev says:

    What about using strings.xml in the resources? That way you can easily add support to different languages :)

    • Mark Allison says:

      I thought of that but there are different syntax rules for constructing the time in different languages so it would require different logic for different languages, not just the string translations. For that reason I decided to stick to English only.

Leave a Reply