Robo Robo - 2 months ago 23
Android Question

Kivy: dimming Android navigation buttons

I would like to dim navigation buttons in Kivy application. How to do it in Java is described here: https://developer.android.com/training/system-ui/dim.html
But I wasn't able to figure out how to do it in Kivy using pyjnuis or something else.
Anyone has done this before?

Thanks

Answer

Yes, you can do this via pyjnius. The only thing to watch out for is that you cannot modify Android UI components from outside the UI thread.

To start with, you'll use pyjnius to import the Java classes:

from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
View = autoclass('android.view.View')

Then the SystemUI can be modified (from the UI thread only):

PythonActivity.mActivity.getWindow().getDecorView()
    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE)

Here is a full example:

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.lang import Builder

from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
View = autoclass('android.view.View')

from android.runnable import run_on_ui_thread

root = Builder.load_string('''
BoxLayout:
    orientation: 'vertical'
    Button:
        text: 'Dim'
        on_press: app.dim()
    Button:
        text: 'Undim'
        on_press: app.undim()
''')

class DimTestApp(App):
    def build(self):
        return root

    @run_on_ui_thread
    def set_systemui_visibility(self, options):
        PythonActivity.mActivity.getWindow().getDecorView().setSystemUiVisibility(options)

    def dim(self, *args):
        self.set_systemui_visibility(View.SYSTEM_UI_FLAG_LOW_PROFILE)

    def undim(self, *args):
        self.set_systemui_visibility(0)

if __name__ == '__main__':
    DimTestApp().run()

You can download a test APK if you'd like (link valid until 2015-04-09).

Comments