[ Web Proxy ]
URL:
Viewing: https://developer.android.com/games/agdk/game-activity/use-text-input [Back]  [Original]

TextInput in GameActivity  |  Android game development  |  Android Developers Skip to main content
Essentials Design & Plan Develop Google Play Blog
Search:
Android Studio
Android Developers [Android Developers]
Send feedback Stay organized with collections Save and categorize content based on your preferences.

TextInput in GameActivity Part of Android Game Development Kit.

GameActivity integrates GameTextInput by:

As shown in the following diagram, applications use different internal logical components for user text input purpose:

alt_text [alt_text]

Note: Bypassing GameActivity and directly creating a GameTextInput instance is out of the scope of this document. For more information about that usage, refer to the GameTextInput documentation.

There are three broad steps to using the built-in GameTextInput library:

The rest of this document describes them in detail. For an example of GameTextInput with GameActivity in action, see the games-samples repository.

Control the soft keyboard on UI

GameActivity provides two functions to control the soft keyboard on the UI:

Refer to the API reference docs for their definitions. After the keyboard is displayed, the applications UI may look similar to the following:

alt_text [alt_text]

Check for text availability

Soft keyboard events get passed from GameTextInput on the Java side to the C/C++ side through the JNI, then travel up to GameActivitys wrapper, finally reflecting in the android_app::textInputState flag implemented in native_app_glue. Applications should poll this flag periodically to perform the intended processing:

Note that android_app::textInputState does not differentiate between single and multiple text input events.

For a simple example, the following code polls the textInputState flag after handling app cycle commands, touch events, and key events:

while (true) {
   // Read all pending events.
   int events;
   struct android_poll_source* source;

   while ((ALooper_pollOnce(engine.animating ? 0 : -1, nullptr, &events,
                                 (void**)&source)) >= 0) {
       // Process this event, etc.
       ...
       // Check if we are exiting.
       if (app->destroyRequested != 0) {
           engine_term_display(&engine);
           return;
       }
   }
   engine_handle_input(app);

   // Process text input events if there is any outstanding.
   if (app->textInputState) {
       // process TextInput events.
          ...
       //reset the textInputState flag
       app->textInputState = 0;
   }
   if (engine.animating) {
         // draw frames.
   }
}

Retrieve the user input text

The input texts and other states are accumulated in GameTextInputs internal buffer, GameTextInput::currentState_. Applications can use one of the following ways to retrieve its content:

Get TextInput state with GameActivity API

Applications acquire the current text input with the typical callback mechanism:

Continuing with the snippet in the previous section, the following code acquires a reference to the text input buffer, processes it (not shown), and resets the event flag:

extern "C" void GameTextInputGetStateCB(void *ctx, const struct GameTextInputState *state) {
    auto* engine = (struct engine*)ctx;
    if (!engine || !state) return;

    // Process the text event(s).
    LOGI("UserInputText: %s", state->text_UTF8);

    // Clear the text input flag.
    engine->app->textInputState = 0;
}

In the game loop shown in the previous section, check and process text with the above text input handler:

if (state->textInputState) {
    GameActivity_getTextInputState(
        app->activity,
        GameTextInputGetStateCB,  // App's event handler shown above.
        &engine // Context to the GameTextInputGetStateCB function.
    );
}

Applications can optionally initialize the GameTextInputState content with GameActivity_setTextInputState().

Get TextInput state with GameTextInput API

Applications can also directly use GameTextInput API to retrieve the current GameTextInputState:

Again, note that applications should not initialize GameTextInput directly; GameActivity already does that during its initialization process.

The callback mechanism is the same as that used by the GameActivitys GameActivity_getTextInputState() function.

References

Developers might find the following resources helpful when creating GameActivity applications:

Feedback

GameActivity and GameTextInput are both part of Jetpack games library. For any issues and questions, create a bug on the Google IssueTracker.

Send feedback

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026-02-26 UTC.

Need to tell us more? [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-02-26 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page