Animation / App Widget / Lockscreen / RemoteViews

TextClock Version 2 – Part 3

In the previous article we added some animations to the Daydream in the TextClock because they were the easier. In this article week look at why it is more tricky to add them to app and lock screen widgets.

widget-smallBefore we look at a method of animating things, it is with thinking a little about why we must go via RemoteViews to access the objects within our layouts as it may have a bearing on how, and indeed if, we should animate things. Android only permits one Activity to be active at any one time and, in an ordinary app, we know that we have full control of the display when we have an active Activity. However, for app widgets, we are not in control of the entire display, only a small section of it. To enable multiple widgets to co-exist, RemoteViews marshalling is used to restrict the level of control each app widget has. This is also true for lock screen widgets where we are co-existing with the unlock controls. Therefore we need to think long and hard about what we want to do within a home screen widget and be mindful that we don’t consume too many resources and processing power. To a degree, having to go through RemoteViews rather enforces this.

In the previous article we were able to use TextSwitcher to perform things for us, but TextSwitcher is not one of the widget types supported by RemoteViews – if we try and use it in our layout we’ll get an error similar to the following:

However, a base class of TextSwitcher *is* supported by RemoteViews: ViewFlipper. It works in a similar way to TextSwitcher, but operates on abstract View objects instead of specific TextView objects. Mechanisms for advancing to the next view are available via RemoteViews, but there is no setFactory() method, so we cannot use the same factory mechanism for creating new Views as we did previously. Instead we’ll create two children in each ViewFlipper during layout inflation, and set the text of them and manually switch between them.

Another issue that we have is that previously we were able to programmatically set an offset for the various animations to start so that we created a more interesting effect when multiple fields were animating together. Once again, we cannot do this via RemoteViews so need a different approach. One again, we’ll do this at layout inflation time, so we need to create two more pairs of slide in and slide out animations: one pair with a short offset, and the other pair with a medium offset:

res/anim/in_left_short_offset.xml:

res/anim/out_left_short_offset.xml:

res/anim/in_left_med_offset.xml:

res/anim/out_left_med_offset.xml:

With these defined, we apply the relevant offset animations to the different ViewFlipper objects in the layout.Out new layouts containing ViewFlippers with offset animations applied, and each containing two TextViews looks like this:

res/layout/appwidget.xml:

res/layout/keyguard.xml:

In the next article in this series we’ll get this all wired up.

The source code for this article can be found here, and TextClock is available from Google Play. Version 2.0.2 has recently been published which contains the changes in this article, but there will be a number of minor updates over the coming weeks as we add additional functionality to the app.

© 2014, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 TextClock Version 2 – Part 3 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 http://blog.stylingandroid.com/license-information.

Leave a Reply

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