Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-1625] events attached to a view not working in Android

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2011-04-17T01:56:41.000+0000
Affected Version/sn/a
Fix Version/sRelease 1.5.0
ComponentsAndroid
Labelsandroid, defect, eventlistener, view
ReporterBurggraf
AssigneeDon Thorp
Created2011-04-15T02:57:43.000+0000
Updated2011-04-17T01:56:41.000+0000

Description

If you attach a click event to a view with addEventListener("click",....), the event does not fire unless you've created children objects inside the view.

Example (this works fine on iOS but not on Android):

var myView = Ti.UI.createView({

top:100,
left:100,
height:40,
width:40,
backgroundColor:'red'

});

win1.add(myView);

myView.addEventListener("click",function(e) {

alert("you tapped the red box!");

});


However, if you add a label to the view,
clicking on the label fires the event:


var myView = Ti.UI.createView({

top:100,
left:100,
height:40,
width:40,
backgroundColor:'red'

});

var myLabel = Ti.UI.createLabel({text:"AAA"});
myView.add(myLabel);

win1.add(myView);

myView.addEventListener("click",function(e) {

alert("you tapped the red box!");

});

Comments

  1. Brion Vibber 2011-04-15

    I ran into this bug today while attempting to attach a click handler to a View to create a larger touch target for a fixed-size icon shown via an ImageView.

    Just want to add that the parent View's events do fire when tapping on the child view (eg the label in the example above, or my ImageView when I tried making it a child of the larger View) but still not on the rest of the parent View.

    I worked around this by instead placing a fixed-size Label with no text on top of the ImageView, and putting the click event on the label.

  2. Burggraf 2011-04-15

    We've noticed a temporary workaround until this is fixed. The "touchStart" event DOES fire, so if you use that instead of the "click" event, you can get the same behavior. (Technically click and touchStart are a bit different, if you were to touch and drag off the object, then release. But for the most part, using touchStart does the trick for us.)

  3. hal 2011-04-15

    In Android I think this issue has something to do with setClickable() returning true for complex views but false for simple views.
    The easiest workaround is to use the borderRadius property to force the view to be complex:

    borderRadius:0

    Then you can use the click event as normal.

  4. Don Thorp 2011-04-15

    Also reported in #952

  5. Don Thorp 2011-04-15

    (from [7b6449b75f82462bd18c1c4776ef67f014e18829]) [#952 state:fixed-in-qa][#1625 state:fixed-in-qa] refactored click handling and removed special cases that weren't needed. Note if a view doesn't handle a click Android search up through all components under the touch/click point to find one that will. If you want to stop other views from getting a click (e.g. full screen loading view) just set an no-op click handler on the topmost view http://github.com/appcelerator/titanium_mobile/commit/7b6449b75f82462bd18c1c4776ef67f014e18829"> http://github.com/appcelerator/titanium_mobile/commit/7b6449b75f824...

  6. Thomas Huelbert 2011-04-15

    g1, 1.6, 2.2 simulator. 1.4.2.5935b3

  7. Thomas Huelbert 2011-04-15

    confirmed - 1.4.2.5935b3 g1 1.6, sim with 2.2. Checked on iOS 4.1

JSON Source