Template

Allows watch face developers to specify a string format, such as %s %d. The string format is very similar to printf() in the C programming language or String.format() in the Java programming language.

Additionally, Template allows the developer to use Android string resources, including the ones declared in res/values/strings.xml.

Introduced in Wear OS 4.

Syntax

<Template>template-string
    <Parameter expression="arithmetic-expression" />
</Template>

Examples

The following code snippet shows how to print the string Current heart rate: 80 using the Template and Parameter elements:

<Template>%s %d
    <Parameter expression="Current heart rate: " />
    <Parameter expression="[HEART_RATE]" />
</Template>

This code snippet shows how to use an Android string resource within an expression:

<Template>%s<Parameter expression="greeting"></Parameter></Template>

where greeting is defined in res/values/strings.xml:

<resources>
    <string name="greeting">Hello, world!</string>
</resources>

This code snippet shows how to use an Android string resource to print the string Day: Fri:

<Template>day_of_week
    <Parameter expression="[DAY_OF_WEEK_S]" />
</Template>

where day_of_week is defined in res/values/strings.xml:

<resources>
    <string name="day_of_week">Day: %1$s</string>
</resources>

Inner elements

The Template element must contain at least one Parameter inner element. The Parameter inner element has one attribute, expression, that is an arithmetic expression whose value is converted to a specific type according to the Template element's format string. The expressions in the different Parameter inner elements are evaluated left-to-right in the Template element's format string.