Starting in Tiles 1.2, you can stream platform data updates using dynamic expressions. You can then associate these updates with animations in your tiles. Your app gets updates to this value every second.
Using dynamic expressions, you don't need to refresh the entire tile when its content changes. To create a more engaging experience in your tiles, animate those dynamic objects.
Associate dynamic expressions with data sources
The androidx.wear.protolayout and androidx.wear.protolayout.material
namespaces contain many classes whose fields accept dynamic expressions. Several
examples include the following:
- Several length values, including the length of an
Arcobject and the length of aCircularProgressIndicatorobject. - Any color, such as the content color of a
Buttonobject. - Many string values, including the content of a
Textobject, the content of aLayoutElementsBuilders.Textobject, and the content description of aCircularProgressIndicatorobject.
To use a dynamic expression as a possible value for an element in your tile, use
the element's corresponding *Prop dynamic property type and pass in the data
source to the dynamic property type's builder class's setDynamicValue()
method.
Tiles support these dynamic property types:
- For linear dimensions, measured in display-independent pixels, use
DimensionBuilders.DpProp. - For angular dimensions, measured in degrees, use
DimensionBuilders.DegreesProp. - For string values, use
TypeBuilders.StringProp. - For color values, use
ColorBuilders.ColorProp. - For floating-point values, use
TypeBuilders.FloatProp.
When you use a dynamic expression that affects physical dimensions—any value in
a tile except for color—you must also specify a set of related constraints, such
as a string format. These constraints allow the system renderer to determine the
maximum amount of space that a value could occupy within your tile. Usually, you
specify these constraints at the element level, not at the dynamic expression
level, by calling a method that starts with setLayoutConstraintsForDynamic*.
The following code snippet shows how to display updates to a heart rate using 3
digits, with a fallback value of --:
override fun onTileRequest(requestParams: RequestBuilders.TileRequest) = Futures.immediateFuture( Tile.Builder() .setResourcesVersion(RESOURCES_VERSION) .setFreshnessIntervalMillis(60 * 60 * 1000) // 60 minutes .setTileTimeline( Timeline.fromLayoutElement( Text.Builder( this, TypeBuilders.StringProp.Builder("--") .setDynamicValue( PlatformHealthSources.heartRateBpm() .format() .concat(DynamicBuilders.DynamicString.constant(" bpm")) ) .build(), TypeBuilders.StringLayoutConstraint.Builder("000").build(), ) .build() ) ) .build() )
Use a small number of expressions within a single tile
Wear OS places a limit on the number of expressions that a single tile can have. If a tile contains too many total dynamic expressions, dynamic values are ignored, and the system falls back to the static values that you provide to the respective dynamic property types.
Consolidate dynamic data into a state object
You can consolidate the latest set of updates from data sources into a state, which you pass over to your tile for value rendering.
To use state information in your tiles, complete these steps:
Establish a set of keys that represent the different values of your tile's state. This example creates keys for water intake and a qqq note:
companion object { val KEY_WATER_INTAKE = intAppDataKey("key_water_intake") val KEY_NOTE = stringAppDataKey("key_note") }
In your implementation of
onTileRequest(), callsetState()and establish initial mappings from each key to a particular dynamic data value:override fun onTileRequest( requestParams: RequestBuilders.TileRequest ): ListenableFuture<Tile?> { // If the tile hasn't had any state set yet, use the default values val state = if (requestParams.currentState.keyToValueMapping.isNotEmpty()) requestParams.currentState else StateBuilders.State.Builder() .setStateMap( dynamicDataMapOf( KEY_WATER_INTAKE mapTo 200, KEY_NOTE mapTo "Good" ) ) .build() return Futures.immediateFuture( Tile.Builder() // Set resources, timeline, and other tile properties. .setState(state) .build() ) }
When you create your layout, in a place where you want to show this data from state, use a
Dynamic*type object. You can also callanimate()to show an animation from the previous value to the current value:val waterIntakeValue = DynamicBuilders.DynamicInt32.from(KEY_WATER_INTAKE)
When needed, you can also update the state with new values. This can be part of a tile's
LoadAction.In this example, the water intake value is updated to
400:val loadAction = loadAction( dynamicDataMapOf( KEY_WATER_INTAKE mapTo 400, KEY_NOTE mapTo "Outstanding" ) )
Recommended for you
- Note: link text is displayed when JavaScript is off
- Migrate to ProtoLayout namespaces
- Get started with tiles
- Other considerations