Dynamically change element content
Stay organized with collections
Save and categorize content based on your preferences.
While Transform
lets you change the appearance of elements or groups of
elements, there might be occasions where you want to switch between a list of
behaviors based on some condition. This is analogous to a switch
statement
or if…else
statement in other languages.
For example, you might want to show a different background for early morning,
morning, lunch, afternoon, evening, and night.
Condition
statements in Watch Face Format allow you to include different parts
of your watch face scene depending on the evaluation of expressions, for
example:
<Condition>
<Expressions>
<Expression name="is_early_morning">
<![CDATA[[HOUR_0_23] >= 6 && [HOUR_0_23] < 8]]
</Expression>
<Expression name="is_morning">
<![CDATA[[HOUR_0_23] < 12]]
</Expression>
...
</Expressions>
<Compare expression="is_early_morning">
<!-- Early morning content here -->
<Group ... />
</Compare>
<Compare expression="is_morning">
<!-- Morning content here -->
<Group ... />
</Compare>
...
<!-- The "else" case -->
<Default>
<!-- content -->
</Default>
</Condition>
A few things to note about conditions:
- The first
Compare
element where the expression
is true
is used, and
others are ignored.
- Owing to the XML format, it can often be easiest to wrap the expression
definition in a
CDATA
element as shown here, as this avoids the need for XML
escaping using entity elements such as >
and &
.
Condition
structures can be nested.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-05-20 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-05-20 UTC."],[],[],null,["# Dynamically change element content\n\nWhile `Transform` lets you change the appearance of elements or groups of\nelements, there might be occasions where you want to switch between a list of\nbehaviors based on some condition. This is analogous to a `switch` statement\nor `if...else` statement in other languages.\n\nFor example, you might want to show a different background for early morning,\nmorning, lunch, afternoon, evening, and night.\n\n`Condition` statements in Watch Face Format allow you to include different parts\nof your watch face scene depending on the evaluation of expressions, for\nexample: \n\n \u003cCondition\u003e\n \u003cExpressions\u003e\n \u003cExpression name=\"is_early_morning\"\u003e\n \u003c![CDATA[[HOUR_0_23] \u003e= 6 && [HOUR_0_23] \u003c 8]]\n \u003c/Expression\u003e\n \u003cExpression name=\"is_morning\"\u003e\n \u003c![CDATA[[HOUR_0_23] \u003c 12]]\n \u003c/Expression\u003e\n ...\n \u003c/Expressions\u003e\n \u003cCompare expression=\"is_early_morning\"\u003e\n \u003c!-- Early morning content here --\u003e\n \u003cGroup ... /\u003e\n \u003c/Compare\u003e\n \u003cCompare expression=\"is_morning\"\u003e\n \u003c!-- Morning content here --\u003e\n \u003cGroup ... /\u003e\n \u003c/Compare\u003e\n ...\n \u003c!-- The \"else\" case --\u003e\n \u003cDefault\u003e\n \u003c!-- content --\u003e\n \u003c/Default\u003e\n \u003c/Condition\u003e\n\nA few things to note about conditions:\n\n1. The first `Compare` element where the `expression` is `true` is used, and others are ignored.\n2. Owing to the XML format, it can often be easiest to wrap the expression definition in a `CDATA` element as shown here, as this avoids the need for XML escaping using entity elements such as `>` and `&`.\n3. `Condition` structures can be nested."]]