Titanium Community Questions & Answer Archive

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

Long press on android, or simulating it with touchstart/end

I'm trying to get long presses to work with table rows on android. Apparently it's not supported with current Titanium releases. Following some samples posted in other Q&As, I came up with the following, but the "touchstart fired" never shows up in the console. So, apparently that event isn't working either. Suggestions?

var holdTime = 1000;
var startTime;
profileTable.addEventListener('touchstart', function(e) {
    Ti.API.info("touchstart fired");
    startTime = new Date();
});
profileTable.addEventListener('touchend', function(e) {
    Ti.API.info("touchend fired");
    var now = new Date();
    if(now - startTime > holdTime) {
        alert(e.rowData.title);
    } else {
        alert(e.rowData.profileID);
    }
    startTime = '';
});
— asked August 23rd 2010 by Tim Poulsen
  • android
  • events
  • mobile
  • tablerow
0 Comments

4 Answers

  • Accepted Answer

    The best way I've been able to simulate a long press on a table view is by the following:

    var touched = false;
    tableview.addEventListener('click', function(e)
    {
        touched = false;
    });
    tableview.addEventListener('touchstart', function(e){
        touched = true;
        setTimeout(function(){
            if (touched)
                alert("Long press");
        },1000);
    });
    tableview.addEventListener('touchmove', function(e){
        touched = false;
    });
    tableview.addEventListener('touchend', function(e){
        touched = false;
    });
    

    Instead of an alert you can use Titanium.UI.OptionDialog to display options.

    — answered December 16th 2010 by Scott Chantry
    permalink
    1 Comment
    • Scott, thanks for sharing your solution. I ended up going with something other than a long press for my app. But, I'll keep this in mind if I decide to add it back in.

      — commented December 17th 2010 by Tim Poulsen
  • Try the code below:

    var tStart;
    profileTable.addEventListener('touchstart', function(e) {
        Ti.API.info("touchstart fired");
        tStart = new Date();
    });
    profileTable.addEventListener('touchend', function(e) {
        Ti.API.info("touchend fired");
        var tEnd = new Date();
        if (tEnd.getTime() - tStart.getTime() > 500) {
            Ti.API.info("Long press");
        }
    });
    

    HTH

    — answered September 3rd 2010 by Virgil Reboton
    permalink
    2 Comments
    • I believe this is more of a "long click" than a "long press". I'm trying to accomplish a "long press" as well. I've tried setting a timeout in touchstart - however, touchstart's event.index is very inaccurate. I really don't get why this is happening.

      — commented October 8th 2010 by Nick Robillard
    • Oops - i realize this is what the asker is doing. The comment above should be to him. I'm sleepy. :S

      — commented October 8th 2010 by Nick Robillard
  • any luck getting this to work guys?

    — answered September 27th 2010 by Luke Haviland
    permalink
    0 Comments
  • any luck getting this to work guys?

    — answered September 27th 2010 by Luke Haviland
    permalink
    1 Comment
    • I apologize but I haven't taken the time to try. I implemented the hasChild:true property on my table rows which let me pop up an option dialog to present the various actions a user could take. I will circle back later to try the long press as I think it will be a bit nicer looking in my app. I'm stuck now getting the GPS code working reliably and have been spending all my efforts there. I can't release my app if the geolocation part doesn't work.

      — commented September 27th 2010 by Tim Poulsen
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.