Create drawable and labels dynamically

Is it possible to create labels and drawable in real time without layouts?
  • Yes it is easy to do:

    You can implement your label as a drawable:
    using Toybox.WatchUi as Ui;
    using Toybox.Graphics as Gfx;

    class MyLabel extends Ui.Drawable {

    var Text, TextAlign, TextColor, Font;

    function initialize(dictionary) {
    dictionary.put(:identifier, "MyLabelDrawable");
    Drawable.initialize(dictionary);
    Text = dictionary[:text];
    TextAlign = dictionary[:textAlign];
    Font = dictionary[:font];
    TextColor = dictionary[:textColor];
    }

    function draw(dc) {
    dc.setColor(TextColor, Gfx.COLOR_TRANSPARENT);
    dc.drawText(locX, locY, Font, Text, TextAlign);
    }
    }


    Then in your view, you can instantiate it as follows:
    function onUpdate(dc) {
    var myLabel = new MyLabel({:locX => 102, :locY => 75, :font => Gfx.FONT_LARGE, :textAlign => Gfx.TEXT_JUSTIFY_CENTER, :textColor => Gfx.COLOR_BLUE, :text => "12:00"});
    myLabel.draw(dc);
    }


    Hope this helps, or at least inspires you to try something that might work for you!