Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

click event on button not firing

I cannot get this button click event to fire. Any ideas would be greatly appreciated.

var btnnext = Titanium.UI.createButton({title:'++ '});
btnnext.width=25;
btnnext.height=25;
btnnext.left=100;

btnnext.addEventListener('click', function(e)
{
    Ti.API.info("testing");
});

winContainer3.add(btnnext);
winContainer2.add(winContainer3);
win2.add(winContainer2);

win2.open();
— asked June 14th 2010 by Rick White
  • click
  • event
0 Comments

4 Answers

  • I think the events fires.
    Try to use Titanium. instead of Ti. inside the function
    Let me know

    — answered June 14th 2010 by Dan Tamas
    permalink
    1 Comment
    • Here is the entire code. The first is app.js. The second is the included file, base_ui.js. I have stripped the code down to just basic stuff. The click event still will not fire. Please help.

      
      // this sets the background color of the master UIView (when there are no windows/tab groups on it)
      Titanium.UI.setBackgroundColor('#000');
      
      
      // create tab group
      var tabGroup = Titanium.UI.createTabGroup();
      
      
      
      //
      // create base UI tab and root window
      //
      var win1 = Titanium.UI.createWindow({
          url:'main_windows/base_ui.js'
      });
      
      
      
      var tab1 = Titanium.UI.createTab({
          title:'',
          window:win1
      });
      
      
      
      //
      //  add tabs
      //
      tabGroup.addTab(tab1);
      
      
      
      
      tabGroup.setActiveTab(1);
      // open tab group with a transition animation
      tabGroup.open({
          transition:Titanium.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT
      });
      

      ///////////////////////////////////

      
      try
      {
          Titanium.UI.setBackgroundColor('#000');
      
      
      
              var winContainer3 = Titanium.UI.createWindow({  
                  title:'Test',
                  backgroundColor:'#fff',
                  height:50,
                  width:200,
                  top:0
              });
              winContainer3.layout='horizontal';
              //--------------------------------------------------//
      
              var win2 = Titanium.UI.createWindow({  
                  title:'test in progress',
                  backgroundColor:'#fff',
                  height:400,
                  width:500,
                  top:50
              });
              win2.layout="horizontal";
      
      
      
              var btnprev = Ti.UI.createButton({
                  title:'previous',
                  width:100,
                  height:100,
                  left:100
              });
      
              btnprev.addEventListener('click', function(){
                  Ti.API.info("testing prev");
              });
      
              var btnnext = Ti.UI.createButton({
                  title:'next',
                  width:100,
                  height:100,
                  left:200
              });
      
              btnnext.addEventListener('click', function(){
                  Ti.API.info("testing next");
              });
      
              winContainer3.add(btnnext);
              winContainer3.add(btnprev);
              win2.add(winContainer3);
              win2.open();
      
      
      
      
      }
      catch(ex)
      {
          Titanium.API.info("error in beginning of baseui " + ex);
      }
      

      — commented June 16th 2010 by Rick White
  • Although your not using a ButtonBar here I found that any event on a button in a ButtonBar would not fire at all if the ButtonBar's width was 'auto' setting it to 100% made the buttons suddenly work! There was no discernible visual difference, just another of Titanium's API inconsistencies that keep popping up.

    — answered March 21st 2011 by Rhys Burnie
    permalink
    1 Comment
    • I had the same problem. Once I removed the Button from the toolbar the it started working.

      — commented July 12th 2011 by David Dehghan
  • I have some issues with my eventListener on a TextField. In my case only focus works. So try:

            btnprev.addEventListener('focus', function(){
                Ti.API.info('Perhaps focus will work?');
            });
    
    — answered March 22nd 2011 by Adis Corovic
    permalink
    0 Comments
  • don't ask me why this works, but it did for me, and it might for you. i had a function

    function create_window(){
        var button = createButton(....);
        button.addEventListener(...)
        var window = createWindow(....);
        window.add(button);
    }
    

    the click wasn't firing. but when i did this:

    var button = createButton(...);
    button.addEventListener(...);
    function create_window(){
        var window = createWindow(....);
        window.add(button);
    }
    

    i'm sure it's got something to do with the contexts and scopes which i have yet to understand completely in titanium, but it works the first way in one of my scripts but only the second way in another script.

    try it on yours - good luck!

    — answered November 27th 2011 by d b
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.