Skip to content

Android Activity

tpecholt edited this page Nov 5, 2023 · 10 revisions

UPDATED 11/06/23

Android Support

As ImGui contains an Android backend implementation ImRAD added support for Android Activities as well. I never liked Android UI development so ImGui seems like a great choice here as well. The support is currently experimental as there are some remaining issues to solve but you can already create functional android apps.

In this tutorial I demonstrate how to create a simple Android app and discuss what more is needed or missing.

Choosing the right IDE

For developing for android we need an IDE which is able to target ARM devices, compile APK, run the code in Android emulator and deploy it to the device.

Visual Studio 2022

On Windows Visual Studio is my IDE of choice and I knew it also contains support for developing on Android so I was naturally inclined to try it out. In VS 2022 installer there is an option for "Mobile development with C++". I installed it and tried to use it but I didn't get very far. There were many issues like error saying Android SDK license agreements were not accepted so open SDK manager but SDK manager is not available from Tools/Android Tools menu and the SDK installation seemed incomplete. Then the wrong version of java was chosen although VS installed newer version so JAVA_HOME had to be specified. Then Native activity which I wanted to use was not available. There are some youtube tutorials from this year but they talk about Native Activity with Ant and all Ant templates were removed in some recent VS update without replacement for gradle alternative. Honestly it looks like VS android support is driven by management who doesn't have clear understanding about developer needs so strange decisions are taken. Fortunately better alternative exist.

Android Studio

Unlike VS Android Studio worked from the beginning without single issue. You can even set key mapping to VS mode to make it more familiar. The android emulator starts really fast. Even deployment to the device is fast and you can connect your phone for debugging via USB or wifi. Things like creating assets directory are easily done from popup menus. It even supports CMake so it's not really necessary to touch gradle.

Android App Structure

Although it may be possible to develop Native Activity in 100% C++ I advise against it. Calling java API through JNI is no fun and can quickly become tedious to write. Therefore it's better to implement Native Activity code in Java (or Kotlin). And add few important methods which wrap desired functionality behind simple API which can be called easily through JNI. Apart from that you need to create Android manifest with certain options set and add C++ shared library into the project.

main.cpp code, java activity code and android manifest can be generated automatically within ImRAD by choosing the last option from the New File Menu. C++ Activity class code can be designed by using the Activity - Android option. Be sure to move these files to their appropriate directories.

image

MainActivity.java

Generated code is based on the original ImGui android example but extended with additional functionality or fixes

Generated MainActivity class contains:

  • Code to load the C++ shared object library

  • showSoftInput() - code for showing or hiding the soft keyboard. The code currently shows different type of keyboards such as standard, number and decimal number input. This is achieved by creating an invisible TextEdit and setting its KeyListener. Different IME types are currently not reported by the generated ImRAD code but it may change in the future version. More needs to be done to support other features such as text prediction.

  • dispatchKeyEvent() - to correctly catch input of unicode characters getUnicodeChar() needs to be called and this is available only in java API so we do it here and store the characters in the queue. In my case for certain characters with diacritics ACTION_MULTIPLE was fired so I tried to process it as well.

  • getDpi() - returns screen DPI for ImGui UI scaling

  • showMessage() - shows a toast message, e.g. informational message which disappears in few seconds

  • pollUnicodeChar() - returns the unicode character press

  • OnKeyboardShown() - although in C++ code we show or hide the keyboard when desired (based on io.WantTextInput value) it is possible the user dismissed the keyboard by pressing the back button. This callback implemented in C++ will be called to react on that. As there is no direct way to know that the code installs GlobalLayoutListener and reacts on visible display frame changes.

AndroidManifest.xml

This is a standard android manifest with few important options:

  • icons, label - specify this for your app

  • theme such as @android:style/Theme.NoTitleBar.Fullscreen. This will let the app to draw to the whole screen minus the title bar on top.

  • android.app.lib_name - specifies the name of the C++ shared object (without the lib prefix)

main.cpp

Clone this wiki locally