Search code examples
javascriptandroidtitaniumappcelerator-mobile

How can I detect if a device has a touch screen in Titanium?


I'm targeting Android with Titanium. How can I tell if a device has a touch screen or not?


Solution

  • This is the solution I came up with. It's really stupid, but it works. Wherever you create your application window, add this code:

    function touchStart(){
        Titanium.App.Properties.setBool("touch", true);
    
        self.removeEventListener("touchstart", touchStart);
    }
    
    self.addEventListener("touchstart", touchStart);
    

    Replace self with the name of your window. Now, the first time a touch event is detected, an application property will be set.

    Later on, check that touch is present with this:

    var touchSupported = Titanium.App.Properties.getBool("touch", false);
    

    If you are testing this on an emulator, the property might persist even when you change "devices". So add this line before anything else:

    Titanium.App.Properties.setBool("touch", false);