Layouts

The resource compiler allows page layouts to be customized to a particular device without changing any Monkey C code. In addition, drawable List objects can also be defined, which are WatchUi.Drawable objects that can draw a number of graphics primitives.

When defining a layout in XML, simply list the drawable objects to include in your layout inside a set of layout tags. Each drawable listed will be turned into Monkey C WatchUi.Drawable objects and drawn one after the other. Because of this, if two drawables in your layout overlap each other the drawable that is defined second will draw on top of the drawable that is defined first. Here is an example of a basic layout:

<resources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://developer.garmin.com/downloads/connect-iq/resources.xsd">
    <layout id="MainLayout">
        <drawable id="MainBackground" />
        <label text="Page Heading" x="10" y="25" font="Gfx.FONT_LARGE" color="Gfx.COLOR_BLACK" />
        <label text="Your information goes here." x="50%" y="50%" font="Gfx.FONT_MEDIUM" color="Gfx.COLOR_DK_GRAY" />
    </layout>
</resources>

To use this layout in your code simply call View.setLayout() inside the View.onLayout() function. Call the parent View.onUpdate() if you plan on using the View.onUpdate() function to update dynamic values on the screen. For example:

class MainView extends WatchUi.View {
    public function onLayout( dc as Dc ) as Void {
        setLayout( Rez.Layouts.MainLayout( dc ) );
    }

    public function onUpdate( dc as Dc) as Void {
        // Call parent's onUpdate(dc) to redraw the layout
        View.onUpdate( dc );

        // Include anything that needs to be updated here
    }
}

Layout

The following attributes are supported by the layout tag:

AttributeDefinitionValid ValuesDefault ValueNotes
idThe handle for the layout. This is used to reference the layout in the Rez moduleAny value that starts with a letterNARequired

Label

Text can be included in layouts. To include text use the label tag, which supports the following attributes:

AttributeDefinitionValid ValuesDefault ValueNotes
idThe handle for the label. The ID provided here references the label defined in the resource XML fileAny value that starts with a letterNA
textThe text to be displayedNAAn empty string
fontThe font to be used when drawing the textSee font referencesGraphics.FONT_MEDIUM
xThe X coordinate of the point that the text will be justified againstpixel value, a relative position using '%', center, left, right, or start0
yThe Y coordinate of the point that the text will be justified againstpixel value, a relative position using '%', center, top, bottom, or start0
justificationHow the text should be justified in relation to the X & Y locationGraphics text justify constantGraphics.TEXT_JUSTIFY_LEFT
colorThe color of the textGraphics color constant or a 24-bit integer of the form 0xRRGGBBGraphics.COLOR_WHITE
backgroundThe background color of the textGraphics color constant or a 24-bit integer of the form 0xRRGGBBGfx.COLOR_TRANSPARENT
visibleThe drawable is visibletrue or falsetrueOnly supported for devices with ConnectIQ 3.3.0 and later

Text Area

Since API level 3.1.0

Text may also be included in a layout as a text area. A text area is similar to a text label, except it will attempt to fit text into the provided area by selecting an appropriate font, adding line breaks, or applying truncation. To include a text area, use a text-area element. The following attributes are permitted:

AttributeDefinitionValid ValuesDefault ValueNotes
idThe handle for the text area. The ID provided here references the text area defined in the resource XML fileAny value that starts with a letterNA
textThe text to be displayedNAAn empty string
fontThe font to be used when drawing the textSee font referencesGraphics.FONT_MEDIUMCannot be used if a font sequence is provided
xThe X coordinate of the point that the text will be justified againstpixel value, a relative position using '%', center, left, right, or start0
yThe Y coordinate of the point that the text will be justified againstpixel value, a relative position using '%', center, top, bottom, or start0
widthThe width of the area to fit the text intopixel value, a relative dimension using '%', or fill0
heightThe height of the area to fit the text intopixel value, a relative dimension using '%', or fill0
justificationHow the text should be justified in relation to the X & Y locationGraphics text justify constantGraphics.TEXT_JUSTIFY_LEFT
colorThe color of the textGraphics color constant or a 24-bit integer of the form 0xRRGGBBGraphics.COLOR_WHITE
backgroundThe background color of the textGraphics color constant or a 24-bit integer of the form 0xRRGGBBGfx.COLOR_TRANSPARENT
visibleThe drawable is visibletrue or falsetrueOnly supported for devices with ConnectIQ 3.3.0 and later

A text area can select a font from a sequence to minimize truncation. The text area will try the fonts in the specified order until the text can be drawn in the given area without truncation. If no font is found that avoids truncation, the last font in the sequence will be used and the text will be truncated. Valid values for each font element are the same as that of the font attribute. An example text-area with a font sequence:

<text-area id="BlockOfText" text="Lorem ipsum dolor sit amet, consectetur adipiscing elit." x="10%" y="10%" width="80%" height="80%">
    <fonts>
        <font>Gfx.FONT_MEDIUM</font>
        <font>@Rez.Fonts.MySmallFont</font>
        <font>Gfx.FONT_XTINY</font>
    </fonts>
</text-area>

Font References

In layout definitions, there are three ways to refer to fonts:

ReferenceDescriptionExample
System font referenceReference the standard FONT enumeration in the Toybox.Graphics module.Graphics.FONT_SMALL
Custom font referenceReference a font in application resources.@Rez.Fonts.MySmallFont
Scalable font referenceReference a system scalable font. This is the font name or names optionally separated by a comma and the pixel size separated by a colon. Alternatively, you can use a Graphics FONT enumeration and a float or percentage scale value. See Scalable Fonts for more information."#BionicBold,Roboto:12" or "#Graphics.FONT_SMALL:50%"

Drawables

Drawables (both bitmaps and drawable XML resources) can also be included in a layout using the drawable tag. The following attributes are supported by the drawable tag:

AttributeDefinitionValid ValuesDefault ValueNotes
idThe handle for the drawable. The ID provided here references the drawable defined in the resource XML fileAny value that starts with a letterNARequired; the drawable must also be defined in a resource XML file
xThe X coordinate of the top left corner in relation to the parent elementpixel value, a relative position using '%', center, left, right, or start0
yThe Y coordinate of the top left corner in relation to the parent elementpixel value, a relative position using '%', center, top, bottom, or start0

Drawable List

Drawable XML resources consist of a list of basic drawables: bitmaps and shapes. To create an XML drawable, define a <drawable-list> in an XML resource file. Both <bitmap> and <shape> tags should be be placed as child nodes inside the <drawable-list>. An example drawable-list:

<resources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://developer.garmin.com/downloads/connect-iq/resources.xsd">
    <drawable-list id="Smiley" background="Gfx.COLOR_YELLOW">
        <shape type="circle" x="10" y="10" radius="5" color="Gfx.COLOR_BLACK" />
        <shape type="circle" x="30" y="10" radius="5" color="Gfx.COLOR_BLACK" />
        <bitmap id="mouth" x="15" y="25" filename="../bitmaps/mouth.png" />
    </drawable-list>
</resources>

To use this drawable in code do the following:

function onUpdate( dc as Dc ) as Void {
    var mySmiley = new Rez.Drawables.Smiley();
    mySmiley.draw( dc );
}

The <drawable-list> tag supports the following attributes:

AttributeDefinitionValid ValuesDefault ValueNotes
idThe ID of the drawableAny string that starts with a characterNARequired
xThe X coordinate of the top left corner in relation to the parent elementpixel value, a relative position using '%', center, left, right, or start0
yThe Y coordinate of the top left corner in relation to the parent elementpixel value, a relative position using '%', center, top, bottom, or start0
widthThe width of the drawable list.pixel value, a relative dimension using '%', or fillfill
heightThe height of the drawable list.pixel value, a relative dimension using '%', or fillfill
foregroundThe color of elements (shapes and text) drawn in the drawableGraphics color constant or a 24-bit integer of the form 0xRRGGBBThe current draw context's foreground color
backgroundThe background color of the drawableGraphics color constant or a 24-bit integer of the form 0xRRGGBBGfx.COLOR_TRANSPARENT

For more, see the Drawable sample app distributed with the SDK.

Shape

The <shape> tag supports the following attributes:

AttributeDefinitionValid ValuesDefault ValueNotes
typeThe type of the shape to be drawnrectangle, ellipse, circle, or polygonNARequired
xFor circles and ellipses: X coordinate of the center of the shape in relation to the parent; for everything else: the X coordinate of the top left corner in relation to the parent element.pixel value, a relative position using '%', center, left, right, or start0
yFor circles and ellipses: Y coordinate of the center of the shape in relation to the parent; for everything else: the Y coordinate of the top left corner in relation to the parent elementpixel value, a relative position using '%', center, top, bottom, or start0
pointsA list of points which defines the polygon[[x1, y1], [x2, y2], ... , [xN, yN]], points can be relative positions using '%'NARequired for polygon; must have at least 3 points
widthThe width of the shape to be drawnpixel value, a relative dimension using '%', or fillfillRequired for rectangle
heightThe height of the shape to be drawnpixel value, a relative dimension using '%', or fillfillRequired for rectangle
aThe a value of the ellipse to be drawnpixel value, a relative dimension using '%', or fillfillRequired for ellipse
bThe b value of the ellipse to be drawnpixel value, a relative dimension using '%', or fillfillRequired for ellipse
colorThe color of the shape to be drawnGraphics color constant or a 24-bit integer of the form 0xRRGGBBThe current draw context's foreground color
corner_radiusThe radius of the rounded corners of the rectanglepixel value0Only valid for rectangle
radiusThe radius of the circlepixel value or a relative dimension using '%'0Required for circle
border_widthThe width of the border around the shapepixel value0Only valid for rectangle, ellipse, and circle
border_colorThe color of the border around the shapeGraphics color constant or a 24-bit integer of the form 0xRRGGBBThe current draw context's foreground colorOnly valid for rectangle, ellipse, and circle

For more see the Drawable sample app distributed with the SDK.

Bitmap

The <bitmap> tag supports the following attributes:

AttributeDefinitionValid ValuesDefault ValueNotes
idThe ID of the drawableAny string that starts with a characterNARequired
xThe X coordinate of the top left corner in relation to the parent elementpixel value, a relative position using '%', center, left, right, or start0
yThe Y coordinate of the top left corner in relation to the parent elementpixel value, a relative position using '%', center, top, bottom, or start0
filenameThe relative path to the image that should be shownA valid, relative pathNARequired
visibleThe drawable is visibletrue or falsetrueOnly supported for devices with ConnectIQ 3.3.0 and later

Custom Drawables

In some cases it is useful to have a drawable entry within a layout point to a custom defined class which extends WatchUi.Drawable, or any of its directly known subclasses. This is possible by using the class attribute of the <drawable> tag.

<layout>
    <drawable id="MoveBar" class="CustomMoveBar" />
</layout>

The drawable entry above will add a new instance of the CustomMoveBar class to the layout. You can then define how the CustomMoveBar is drawn by overriding the draw(dc) function.

import Toybox.WatchUi;

class CustomMoveBar extends WatchUi.Drawable {
    function draw(dc as Dc) as Void {
        // Draw the move bar here
    }
}

Passing Parameters to Custom Drawables

If may be useful to pass values into a custom Drawable. To do this you define <param> children within the <drawable> tag. The <param> tag should define the name of the parameter using the name attribute and the value as it's content. The content value is passed as is to the custom Drawable. This means if you want to pass a string you must wrap the content in quotes.

<layout>
    <drawable id="MoveBar" class="CustomMoveBar">
        <param name="color">Gfx.COLOR_RED</param>
        <param name="string">"Hello Custom Drawable!"</param>
    </drawable>
</layout>

The parameters defined in XML are passed to the custom Drawable's initialize function as a dictionary. The names are passed as symbols and the values are passed as they appear in the XML.

import Toybox.WatchUi;

class CustomMoveBar extends WatchUi.Drawable {

    private var _color, _string;

    public function initialize(params as Dictionary) {
        // You should always call the parent's initializer and
        // in this case you should pass the params along as size
        // and location values may be defined.
        Drawable.initialize(params);

        // Get any extra values you wish to use out of the params Dictionary
        _color = params.get(:color);
        _string = params.get(:string);
    }
}