Search code examples
javascriptandroidnativescript

How do I add an event for a button via code in NativeScript?


I started learning Native Script a few weeks ago, and honestly, I don't like creating an interface through XML, so I want to create it directly through code. Everything was going well until I got to the events.

I have a button:

const btn = new Button();

How do I add an event to it that triggers when I click on it? I know how to do it in XML format, but I want to add an event just like that.


Solution

  • You can do that by adding an event listener - see https://v6.docs.nativescript.org/core-concepts/events

    I think you'll find after continuing down this path for a while that defining the ui in xml is actually much easier, but your choice.