Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-1641] Android: Support Event Handlers on Hardware Buttons

GitHub Issuen/a
TypeNew Feature
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2011-04-17T01:56:44.000+0000
Affected Version/sn/a
Fix Version/sRelease 1.5.0
ComponentsAndroid
Labelsandroid, feature
ReporterDon Thorp
AssigneeDon Thorp
Created2011-04-15T02:58:06.000+0000
Updated2011-06-21T14:30:30.000+0000

Description

Add support for overriding hardware buttons on Android devices. Not all devices will have all buttons. The following buttons should be supported.

Back, Camera, Focus (Half-Press of Camera), Search, Volume Up, and Volume Down.

If an event is added for any of these buttons, it takes sole responsibility for the action of the button. There will be no method to conditionally forward to the original handler. Removal of the event listener will restore default handling.

These handlers are allowed on Ti.UI.Window and will only function when attached to "heavyweight" windows. When Ti.Android.Activity is released handlers will be settable on Ti.Android.currentActivity.

No additional data is passed in the event object.

Example.

win.addEventListener('android:back', function(e) {
    Ti.API.Info("Pressing Back Will Not Close The Activity/Window");
});

Event Names:
android:back, android:camera, android:focus, android:search, android:volup, android:voldown

Comments

  1. Don Thorp 2011-04-15

    Sample used during dev. http://github.com/donthorp/androidng/blob/b078177e2386bf4149fb7f37b607147921eb21bf/assets/Resources/android-keyboard.js"> android-keyboard.js

  2. Don Thorp 2011-04-15

    (from [b59fa44d54329b101c639252792968b10ba1113b]) [#1641 state:fixed-in-qa] implemented. Events are fired on key up, down and repeat are ignored. Adding a listener shortcuts the key being passed on for default processing. http://github.com/appcelerator/titanium_mobile/commit/b59fa44d54329b101c639252792968b10ba1113b"> http://github.com/appcelerator/titanium_mobile/commit/b59fa44d54329...

  3. Don Thorp 2011-04-15

    (from [d62b2f33f5d5397758b06a33637e92ecd88694f6]) [#1641 state:fixed-in-qa] implemented. Events are fired on key up, down and repeat are ignored. Adding a listener shortcuts the key being passed on for default processing. http://github.com/appcelerator/titanium_mobile/commit/d62b2f33f5d5397758b06a33637e92ecd88694f6"> http://github.com/appcelerator/titanium_mobile/commit/d62b2f33f5d53...

  4. Thomas Huelbert 2011-04-15

    thanks for the test content. Confirmed on simulator and device (though on the nexus I did not have a camera button, so camera and focus were not tested).

  5. Paul Dowsett 2011-06-21

    For those that need it, here's a working example:
       Ti.UI.setBackgroundColor('#000');
       
       var tabGroup = Ti.UI.createTabGroup();
       
       var win1 = Ti.UI.createWindow({
         backgroundColor:'white'
       });
       var tab1 = Ti.UI.createTab({
         title:'Tab 1',
         window:win1
       });
       
       var label1 = Ti.UI.createLabel({
         color:'#999',
         text:'click me',
         top:20
       });
       win1.add(label1);
       
       label1.addEventListener('click', function(){
         var winHeavy = Ti.UI.createWindow({
           title:'Heavywight Window',
           backgroundColor:'blue',
           fullscreen:false // this causes the window to be heavyweight
         });
         winHeavy.addEventListener('android:back', function(e) {
           Ti.API.info("Pressing Back Will Not Close The Activity/Window");
         });
         var label2 = Ti.UI.createLabel({
           color:'white',
           text:'Pressing the back button will test the Android back button event. Click this label to close this heavyweight window',
           top:20
         });
         winHeavy.add(label2);
         winHeavy.open();
       });
       var win2 = Ti.UI.createWindow({
         title:'Tab 2',
         backgroundColor:'green'
       });
       var tab2 = Ti.UI.createTab({
         title:'Tab 2',
         window:win2
       });
       
       tabGroup.addTab(tab1);
       tabGroup.addTab(tab2);
       
       tabGroup.open();
       

JSON Source