Skip to content
Advertisement

Python: how do I can use android.show_keyboard for my android app?

I wrote a small pygame app that fills random colors on the device’s screen:

import sys, os
andr = None # is running on android
try:
    import android
    andr = True
except ImportError:
    andr = False
try:
    import pygame
    import sys
    import random
    import time
    from pygame.locals import *
    pygame.init() 
    fps = 1 / 3  # 3 fps
    width, height = 640, 480
    screen = pygame.display.set_mode((width, height), FULLSCREEN if andr else 0) # fullscreen is required on android
    width, height = pygame.display.get_surface().get_size() # on android resolution is auto changing to screen resolution
    while True:
        screen.fill((random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.flip()
        time.sleep(fps)
except Exception as e:
    open('error.txt', 'w').write(str(e)) # Save error into file (for android)

But there are no UI elements (like in kivy) (but I can draw them), so I want to show/hide the keyboard from code. But I can’t find docs about android.show_keyboard and android.hide_keyboard

My attempts:

  1. When I call android.show_keyboard(), I get an error saying that 2 args are required
  2. When I add random args: android.show_keyboard(True, True), I also get an error saying that var input_type_ is not global
  3. When I change the 2nd arg to a string: android.show_keyboard(True, 'text'), the app just crashes without saving the error to file.

Can someone help me with how I can show/hide the keyboard?

Advertisement

Answer

As specified in the Python for Android documentation, android is a Cython module “used for Android API interaction with Kivy’s old interface, but is now mostly replaced by Pyjnius.”

Therefore, the solution I have found is based on Pyjnius, and essentially consists in replicating the Java code used to hide and show keyboard on Android (I used this answer as a base, but there might be something better out there), by exploiting the Pyjnius autoclass-based syntax:

from jnius import autoclass


def show_android_keyboard():
    InputMethodManager = autoclass("android.view.inputmethod.InputMethodManager")
    PythonActivity = autoclass("org.kivy.android.PythonActivity")
    Context = autoclass("android.content.Context")
    activity = PythonActivity.mActivity
    service = activity.getSystemService(Context.INPUT_METHOD_SERVICE)
    service.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)


def hide_android_keyboard():
    PythonActivity = autoclass("org.kivy.android.PythonActivity")
    Context = autoclass("android.content.Context")
    activity = PythonActivity.mActivity
    service = activity.getSystemService(Context.INPUT_METHOD_SERVICE)
    service.hideSoftInputFromWindow(activity.getContentView().getWindowToken(), 0)

If you want to learn more about how Pyjinius’s autoclass works, take a look at the section related to Automatic Recursive Inspection, within the Python for Android documentation.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement