How to create a button in a Widget to call a function

Using the Vivoactive 4

SDK = 3.2.0

I'm attempting to create a widget with a button, then having that button run a specific function/action. I've searched the forums and followed the advice given here: Any one have a working example of a hand coded button? - Discussion - Connect IQ - Garmin Forums

The simulator and widget runs from that example, and the button appears. I'm able to click the button and it changes color correctly according to the states it has been assigned, but it will not run the function I've created onButtonPressed. So nothing shows up in the console log.

Below is how I've setup the View and Delegate files. Is there anything I might be doing wrong or missing to get it to work? I feel as though I'm missing something to connect the delegate properly to the button.

View.mc

-------------------------
import Toybox.Graphics;
import Toybox.WatchUi;
import Toybox.Communications;

class View extends WatchUi.View {

    function initialize() {
        View.initialize();
    }

    // Load your resources here
    function onLayout(dc as Dc) as Void {
        //setLayout(Rez.Layouts.MainLayout(dc));
        var b_zout = {
        :locX => 100,
        :locY => 100,
        :width => 100,
        :height => 70,
        :stateDefault => Graphics.COLOR_ORANGE,
        :stateHighlighted => Graphics.COLOR_YELLOW,
        :stateSelected => Graphics.COLOR_BLUE,
        :stateDisabled => Graphics.COLOR_ORANGE,
        :background => Graphics.COLOR_WHITE,
        :behavior => :onButtonPressed
        };
        var tsButton = new WatchUi.Button(b_zout);

        setLayout([ tsButton ]);
    }

    // Called when this View is brought to the foreground. Restore
    // the state of this View and prepare it to be shown. This includes
    // loading resources into memory.
    function onShow() as Void {
    }

    // Update the view
    function onUpdate(dc as Dc) as Void {
        // Call the parent onUpdate function to redraw the layout
        View.onUpdate(dc);
    }

    // Called when this View is removed from the screen. Save the
    // state of this View here. This includes freeing resources from
    // memory.
    function onHide() as Void {
    }
Delegate.mc
-------------------
import Toybox.WatchUi;

class Delegate extends WatchUi.BehaviorDelegate {

function initialize() {
BehaviorDelegate.initialize();
}

function onButtonPressed() {
    System.println("Test");
    return true;
}

}
  • There is no onButtonPressed callback, so that code will never get called.

    The way I do screen buttons is that I useOnTap,along with the x/y/width/height of the button, andcheck the tap if it's within that area.

    Unless you want to lock yourself into touch devices, look at using things like onMenu, onSelect, onNextPage, etc, which can be used with both button and touch devices.