Skip to content

Instantly share code, notes, and snippets.

@OptimusGREEN
Last active February 8, 2019 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OptimusGREEN/6bf145008408aeab366d88d7f62ba072 to your computer and use it in GitHub Desktop.
Save OptimusGREEN/6bf145008408aeab366d88d7f62ba072 to your computer and use it in GitHub Desktop.
Kivy/Buildozer Android: Change theme in manifest during build

This automatically changes the default theme in the android_manifest.xml to the theme specified in the var "NEW_THEME" using p4a.hooks

The example I used is here https://gist.github.com/tito/4f75385054222182e4f0cf56c90dcdf8 with further instructions in how to implement it.

This hook_theme.py file is the 'hook_something.py' mentioned in Tito's example.

It should be noted that the appropriate gradle dependency for you're chosen theme will need added to the buildozer.spec

mine in this case is android.gradle_dependencies = 'com.android.support:appcompat-v7:26.0.0-alpha1'

import re
from pythonforandroid.logger import info
"""
HOOK for Android Theme
"""
NEW_THEME = "@style/Theme.AppCompat.Light"
def after_apk_build(toolchain):
# mutate AndroidManifest.xml
manifest_fn = "{}/src/main/AndroidManifest.xml".format(
toolchain._dist.dist_dir)
with open(manifest_fn, "r") as fd:
manifest = fd.read()
OLD_THEME = str(re.search(r'android:theme="(.*?)"', manifest).group(1))
# check if new theme is already active
if NEW_THEME != OLD_THEME:
# changing to new theme
info("CHANGIN THEME: replacing: {} with {}".format(OLD_THEME, NEW_THEME))
manifest = manifest.replace(OLD_THEME, NEW_THEME)
# saving changes
with open(manifest_fn, "w") as fd:
fd.write(manifest)
else:
info(NEW_THEME + "already in manifest")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment