[TIMOB-1625] events attached to a view not working in Android
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2011-04-17T01:56:41.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 1.5.0 |
Components | Android |
Labels | android, defect, eventlistener, view |
Reporter | Burggraf |
Assignee | Don Thorp |
Created | 2011-04-15T02:57:43.000+0000 |
Updated | 2011-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!");
});
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.
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.)
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.
Also reported in #952
(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...
g1, 1.6, 2.2 simulator. 1.4.2.5935b3
confirmed - 1.4.2.5935b3 g1 1.6, sim with 2.2. Checked on iOS 4.1