Android Wear / ViewStub

Match Timer – Part 5

Previously in this series we’ve looked at the timer engine, the event engine, and the mechanism for notifying the user in our app for timing football matches. In this article we’ll turn our attention to the UI and look at the Activities in the app.

matchtimerOn a mobile or tablet notifications can be a useful mechanism to alert a user to new or updated information in an app. However on Wear notifications are much more front and centre and are the primary mechanism for user interaction. Whereas on mobile & tablet you have a launcher app which is the primary mechanism for launching apps, on Wear the launcher has been deliberately pushed away to encourage developers to use alternate mechanisms to launch Wear apps (we’ll cover this in a future series). For Match Timer, we’ll need to use voice control or the launcher to initially display a Notification. The main Launcher Activity is actually pretty simple:

All this does is call MatchTimerReceiver.setUpdate() which we defined previously which will create a Notification which the user can then interact with.

There is a second Activity in our app which displays the detailed information within the Notification (we discussed how this gets attached to the notification in the previous article). This Activity has a UI associated with it and our main layout contains a single WatchViewStub:

This is a custom ViewStub for Wear which will automatically inflate one of two distinct layouts depending on the shape of the Wear device. There are also two further layouts which contain the same controls, but in layouts optimised for the different from factors. Here’s the rectangular layout:

This has three TextViews which will display the total elapsed time, total played time, and total stoppage time. The round layout has the same Views, with the same IDs but just in a slightly different layout which will work better on a round display. I won’t bother listing the round layout here, it will be available in the source.

Finally we have MatchTimerNotificationActivity itself:

This is pretty straightforward, we use a Handler to fire every second while the Activity is running. There’s a couple of aspects that are worthy of a little explanation, though. Firstly, because we’re using a ViewStub, the actual layout containing the controls that we need to update will not exist after we inflate the activity_match_timer layout. So we need to use a ViewStub.OnLayoutInflatedListener so that we receive a callback once the ViewStub‘s layout has been inflated. At that point we’re able to get our View handles and perform an initial update.

The second thing worth mentioning is that initially all of the user interaction was done via the Notification actions. While this worked quite well, I found during testing that in the excitement of a live football match having to swipe to get to the actions was a little cumbersome. I therefore decided to add an OnClickListener which would perform the most common action (i.e. the action on the first page to the right of the main activity) whenever the user taps on the Activity. This made thing much better, but I also discovered that by adding this, it also handled taps on the Notification itself (i.e. when just the notification title is displayed), and this made things easier still!

That’s pretty much it for the code, all that we need is the manifest and we’re good to go:

As this is a Wear app we must declare that in a uses-feature element. We also need to declare that we require VIBRATE permission otherwise our vibrating alarms will not work. There are declarations for both Activities. We don’t need to do anything in order to enable voice launch capabilities, other than adhere to the normal rules (i.e. the appropriate intent-filter)for creating a launch-able Activity – the framework adds voice launching based upon the name used in the android:label attribute.

Finally we declare MatchTimerReceiver with the supported actions.

When I originally planned this series of articles, I felt that this would be the appropriate place to conclude the series. However in releasing the app to Google Play I encountered one or two issues in packaging the app appropriately for upload to the Play developer console. So in the concluding article in this series we’ll take a look at how to package the app ready for distribution.

Match Timer is available on Google Play.

Get it on Google Play

© 2014, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Match Timer – Part 5 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at

Leave a Reply

Your email address will not be published. Required fields are marked *