dc.clear() does not remove/hide elements

Hey,

in my onUpdate() function I am using dc.clear() to remove all elements before I decide in an if-statement which element to show depending on a variable. However, all elements are shown at all times, overlapping each other.

  • All elements (bitmaps) are listed in the layout.xml
  • The bitmaps are linked to variables (icon1 and icon2) in the onLayout function
  • The onUpdate function is triggered successfully (checked with print-statement).

function onUpdate(dc as Dc) as Void {
    // Call the parent onUpdate function to redraw the layout
    View.onUpdate(dc);
        
    dc.clear();
    
    if (phase == 1) {
        _icon1.draw(dc);
    }
    if (phase == 2) {
        _icon2.draw(dc);
    }
}
    

Any advise?

Thanks and have a great day :)

  • 1. You need to call dc.setColor before clear.

    2. onUpdate will draw everything that's in your layout.

    3. would maybe help if you include your layout's relevant part

  • setColor() fixed it. Thank you so much!

  • Is there any point in calling View.onUpdate() if it's immediately followed by dc.clear()? Seems pointless to draw the layout just to clear the screen afterwards. This could impact battery life (especially if the app is a watch face.)

  • Good point. It's my very first app (not watch face) and I don't know yet the best practices with respect to battery life and performance.

    On button click I want to hide an icon and show another icon instead. It does work with dc.clear() but I am happy to receive tips on how to do it better Slight smile

    Since the icons are at the exact same position I was thinking of having just one element and change its content. But I don't know how to do this.

  • I think dc.clear() is fine in your example, but it seems that calling onUpdate() prior to that is a waste of time.

    Then again, the current scheme might get messy if you have other stuff in the layout beside the two icons. Then you'd have to manually call draw() for every other element in the layout.

    What you could try doing is setting the visibility of the icons before onUpdate(), using Drawable.setVisible().

    e.g.

    function onUpdate(dc as Dc) as Void {
        _icon1.setVisible(false);
        _icon2.setVisible(false);
        if (phase == 1) {
            _icon1.setVisible(true);
        } else if (phase == 2) {
            _icon2.setVisible(true);
        }
    
        // render layout
        View.onUpdate(dc);
    }