Xperia™ PLAY touch pad

Are you a game developer? The new Xperia™ PLAY features an analog touch pad for full gaming experiences. And the good thing is, you will be able to utilize it for your games too! You can use native code, that is, code written in C or C++, in conjunction with the Android™ Native Development Kit (NDK) to access touch events from the Xperia™ PLAY touch pad. Please note that you cannot use the Android SDK to access these touch events.

There are two important elements in using native code to handle touch events from the Xperia™ PLAY touch pad: the NDK and the NativeActivity class in the Android SDK. The NDK lets you create native code for your Android application. The NativeActivity class enables you to write an Android activity in native code. In addition, the NativeActivity class handles the communication between the Android framework and your native code. However, because the native activity runs on the same thread as your Android application, you’ll need to take actions in your code to prevent the native activity from blocking the application. This could force you into some tricky thread synchronization operations.

But there’s a solution. The NDK offers a static helper library named native-app-glue that handles the thread creation for you. Beyond that, the helper library interface provides a facility called a looper that listens for activity lifecycle events as well as input events coming from the input queue attached to the activity.

See how simple it is to use the native-app-glue library and the looper to detect and process touch events from the Xperia™ PLAY touch pad. Download the full tutorial below and find out.

More information:

Sort by

  • By shirley
    5th March 2011.
    01:11

    Hi, I have a question.
    I’ve already had a Android SDK code, but I need to implement the touchpad. You says I need to use NDK to do touchpad, but I still want to keep SDK code. So what’s the better way to do this?
    I’m thinking some ways:
    1. Use NativeActivity, but then my own Activity won’t work and I don’t know how to call a java’s view from NDK code.
    2. Keep the own SDK code and call NDK method from SDK code. But it seems the inputqueue can only get when I implement NativeActivity? (I don’t know about the JNI code…)

    So can you give me some suggestion how to do this?
    Thanks.

    Thumb up 1 Thumb down 0

  • By Ed Ort
    8th March 2011.
    18:18

    @shirley: Yes, you’re correct. You need to use a NativeActivity instead of an Activity. It’s best to use the NativeActivity with the native-app-glue helper library, as described in the tutorial. This avoids any threading issues related to using the nativeActivity class. Note that you can’t call down to the native code with JNI. You need to use the Native activity and everything else that comes with it on the native (C/C++) side.

    As far as how to use the NDK code with Java, see the TouchpadNAJava code example that is packaged with the Tutorial. There are three code examples packaged with the Tutorial:

    TouchpadNAGlue: Uses a NativeActivity and the native-app-glue helper library.
    TouchpadNAJava: Uses a NativeActivity and a Java class.
    TouchpadNA. Uses a NativeActivity (no helper library).

    Thumb up 0 Thumb down 1

  • By Shirley
    9th March 2011.
    01:29

    Hi,
    thanks for your answer, but I still have trouble making it work.
    When I create a view and a Thread in Java class, the program just run NDK’s thread instead of the Java’s thread I created.
    So how should I make a multi-view, one to run Java’s thread and show grphics and the other to run NDK’s thread to get control pad response?
    Thanks again.

    Thumb up 0 Thumb down 0

  • By Ed Ort
    10th March 2011.
    18:15

    @Shirley: The best advice I think I can give you is to examine the code in the TouchpadNAJava example.You can find the link to the samples download in the More Information section at the end of this article.

    Thumb up 0 Thumb down 1

  • By Sponmagnet
    13th April 2011.
    18:20

    hello SE Developers. Why Dont you make a computer program that convert ps1 games that we have on disks & images instead of re-programing the whole ps1 game that you own in this way users can convert their own games into xperia play & they will be satisfied for a period of time, & your mobile phone will be in the market for a will before it die like other SE phones did, this would be a win-win situation for both USERS & YOU. OR at least you can release an open project for the same purpose & Third Party DEVs will do the job. Or your will phone will be hacked by hackers for the same purpose. You Know we all love the old PS1 games but the problem is no body will buy a game that he already have,why would i pay 3-6$ for game that i have & can play on my PSP/Ps3/&PC for free???!!!
    So Please Please Take My advise & Make that Conversion Program its much better for all of US.

    WITH THANKS: YOUR faithful USER

    Thumb up 0 Thumb down 0

  • By Ed Ort
    20th April 2011.
    22:05

    @Sponmagnet. We appreciate your suggestions. However, this is really a request you need to direct to Sony Computer Entertainment, which is responsible for PS1 games, not Sony Ericsson. Sony Computer Entertainment and Sony Ericsson are two different companies.

    Thumb up 0 Thumb down 0

    • By Jeffrey Blattman
      22nd August 2011.
      18:05

      these examples cover one use case … you already have a native activity and you want to integrate the touchpad. that might be the case for some games but for the vast majority of other applications this doesn’t apply. there are many non-game, non-native applications that could make interesting uses of the touchpad.

      i’d like to see an example of how to integrate the touchpad into a non-native activity.
      thanks.

      Thumb up 0 Thumb down 0

      • By Joe Padre
        1st September 2011.
        22:59

        Hi Jeffrey,
        Thanks for your feedback. I’m working with one of our Engineers in Developer Support to get you an example, and we’ll post it as soon as possible. Stay tuned.
        Regards,
        Joe from Developer World.

        Thumb up 0 Thumb down 0

  • By Kevin Baker
    9th August 2011.
    12:43

    Hello, I have a question about the touchpads.

    Why is it implemented in such a clunky and awkward way? You are making it extremely difficult to add to existing apps, especially for indie developers like me who cannot afford to purchase an Xperia Play just to check their NDK code works.

    Here is a suggestion as to how you should have implemented it.

    If you touch the pad, it generates a touch event (same as if a finger was touching the screen). The touch position would be a position on the screen, circular, as big as the screen. Eg. if you touched ’9 o’ clock’ position on the touchpad, a touch would be generated a x=0, y=240 (landscape mode) touch the centre of the touchpad and you’ll get a touch event at 480,240. Likewise touch the second pad and you get 2 touch events. The second touch pad can generate events x = -840 to -0 or something like that, to differentiate the two touch pads.

    That way it would be trivial for existing apps to incorporate your touchpad without all this NDK nonsense. Apps would work in the SDK as it was intended. Basically you did this already for the buttons, so you started out like it but didn’t finish the job.

    This is a massive fail for you and if you’d thought about it a bit more you could have implemented the above very easily. I was quite excited about getting my game working on one, but now it is unlikely to happen. It means I have to purchase one and with the level of sales it would take a while to recoup the cost. If you would like to ‘loan’ me an Xperia Play for this purpose that would be okay ;)

    Thumb up 5 Thumb down 0

    • By Joe Padre
      1st September 2011.
      22:56

      Hi Kevin,
      Thanks for your feedback. Sony Ericsson has implemented the second touch pad through the method that was available in the existing framework provided by the Android platform. We have created multiple samples and tutorials to help developers take advantage of the device’s capabilities as easily as possible and have two programs to provide devices at no cost to Indie developers such as yourself. The first is the Device Loaner Program, through which we will loan you a device for 30 days to test your implementation on the device. The second is the Stand Out Campaign, where you can submit an application and if chosen for participation receive a free device to keep when you implement the device hardware into your game’s controls.
      Regards,
      Joe from Developer World

      Thumb up 0 Thumb down 0

  • By Alfred Burns
    6th January 2012.
    22:13

    Is it just me or is the tutorial pdf completely borked? Most of the text looks like ASCII jargon and the only legible text is bits and pieces of code.

    Thumb up 0 Thumb down 0

    • By Joe Padre
      25th January 2012.
      00:39

      Mr. Burns,
      I downloaded the PDF and seems bork-free to me. Could you give it another go? Otherwise, I can send you the PDF directly.
      Best Regards,
      Joe from Developer World

      Thumb up 0 Thumb down 0