Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:19:24+00:00 2026-06-12T13:19:24+00:00

After reading the related docs I don’t get if I can create things like

  • 0

After reading the related docs I don’t get if I can create things like buttons or other UI elements used to get user inputs with just the use of C++/C code compiled with the NDK.

There are no problems when i want to handle a “window” or activity that needs to stay in focus, but I don’t get how to build an UI with elements for callbacks and user input.

It’s strange that there is a windowing framework in place but without any trace of callbacks for UI elements.

Can I build touch buttons or a virtual gamepad with the NDK ?


I appreciate the effort and the fact that we are getting closer to my point but apparently i wasn’t explaining myself good enough.

I found this image here
enter image description here

Now my problem and the focus of this question is:

Supposing that i can place and draw this virtual joystick, how i can detect only the movements and have a callback like Joystick.onUp or Joystick.onDown with Android and using only the NDK ?

If there are no callbacks of this kind available from the NDK, I’m supposed to detect the [x,y] position everytime, compare it to the location of my joystick, store the previous position, compare the previous position and the next one to get the direction ?

Since the sensor throws events at a really fast rate, i think that building this on my own considering only the raw X,Y couple, will end up in having a really inefficient control system because it will not be optimized at OS level with the appropriate sensor calls.

According to the NativeActivity example it’s also unclear how to handle multiple touch point, for example how i can handle 2 touch event at the same time ?

Just consider the image above and think about having only x,y coordinate for 1 touch point and how i can solve this in an efficient way that is supported by the NDK.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:19:25+00:00Added an answer on June 12, 2026 at 1:19 pm

    Sure you can. Read up on calling Java from C(++), and call the respective Java functions – either construct UI elements (layouts, buttons, etc.) one by one, or load an XML layout. There’s no C-specific interface for that, but the Java one is there to call.

    Unless it’s a game and you intend to do your own drawing via OpenGL ES. I’m not sure if you can mix and match.

    In a NativeActivity, you can still get a pointer to the Java Activity object and call its methods – it’s the clazz member of the ANativeActivity structure that’s passed to your android_main as a parameter, via the android_app structure. Take that pointer, take the JNIEnv* from the same, and assign a layout.

    How will this interoperate with OpenGL drawing, I’m not sure.

    EDIT: about putting together your own input processing. The key callback is onInputEvent(struct android_app* app, AInputEvent* event) within android_app structure. Place your callback there, Android will call it whenever appropriate. Use AInputEvent_getType(event) to retrieve event type; touch events have type AINPUT_EVENT_TYPE_MOTION.

    EDIT2: here’s a minimum native app that grabs the touch events:

    #include <jni.h>
    #include <android_native_app_glue.h>
    #include <android/log.h>
    
    static int32_t OnInput(struct android_app* app, AInputEvent* event)
    {
        __android_log_write(ANDROID_LOG_ERROR, "MyNativeProject", "Hello input event!");
        return 0;
    }
    
    extern "C" void android_main(struct android_app* App)
    {
        app_dummy();
        App->onInputEvent = OnInput;
    
        for(;;)
        {
             struct android_poll_source* source;
             int ident;
             int events;
    
             while ((ident = ALooper_pollAll(-1, NULL, &events, (void**)&source)) >= 0)
             {
                 if(source != NULL)
                     source->process(App, source);
    
                 if (App->destroyRequested != 0)
                         return;
             }
        }
    
    }
    

    You need, naturally, to add a project around it, with a manifest, Android.mk and everything. Android.mk will need the following as the last line:

    $(call import-module,android/native_app_glue)
    

    The native_app_glue is a static library that provides some C bridging for the APIs that are normally consumed via Java.

    You can do it without a glue library as well. But then you’ll need to provide your own function ANativeActivity_onCreate, and a bunch of other callbacks. The android_main/android_app combo is an interface defined by the glue library.

    EDIT: For touch coordinates, use AMotionEvent_getX/Y(), passing the event object as the first parameter and index of the pointer as the second. Use AMotionEvent_getPointerCount() to retrieve the number of pointers (touch points). That’s your native processing of multitouch events.

    I’m supposed to detect the [x,y] position everytime, compare it to
    the location of my joystick, store the previous position, compare the
    previous position and the next one to get the direction ?

    In short, yes, you are. There’s no builtin platform support for virtual joysticks; you deal with touches and coordinates, and you translate that into your app’s UI metaphor. That’s pretty much the essence of programming.

    Not “everytime” though – only when it’s changing. Android is an event-driven system.

    Now, about your “I want it on the OS level” sentiment. It’s WRONG on many levels. First, the OS does not owe you anything. The OS is what it is, take it or leave it. Second, unwillingness to extend an effort (AKA being lazy) is generally frowned upon in the software community. Third, the OS code is still code. Moving something into the OS might gain you some efficiency, but why do you think it will make a user perceptible difference? It’s touch processing we’re talking about – not a particularly CPU intensive task. Did you actually build an app, profile and find its performance lacking? Until you do, don’t ever guess where the bottleneck would be. The word for that is “premature optimization”, and it’s something that everyone and their uncle’s cat would warn you against.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

after reading all the questions related to this I still can't seem to figure
After reading this question, I need to clear up some things. IQueryable<Customer> custs =
After reading jQuery's CSS documentation , it doesn't look like it offers any advantages
I am new to JavaScript and after reading related books for quite some time,
After reading many related posts on this site on the subject of image comparison
After reading a lot of articles and many answers related to the above subject,
After reading a few related questions I've decided to use the tpl library to
After reading a question related with the performance of sin/cos ( Why is std::sin()
Edit 3 After reading a boat load I really don't think with any ORM
After reading related questions #1 , #2 I still haven't found an answer to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.