Android Tutorial Changing the theme at runtime

Using the hooks feature it is possible to change the Activity theme at runtime.

The theme is set before the activity is actually shown. This means that in order to change the theme we need to destroy the activity and start it again. You can use StateManager to save the activity state.


The hook code is:
B4X:
#if java
public void _onCreate() {
   if (_theme_value != 0)
     setTheme(_theme_value);
}
#end if
There must be a process global int variable named Theme_Value. Note that the underscore is important. It prevents the obfuscator from changing the variable name which will cause the inline Java code to break.

Updates

Example updated and the selected theme value is stored in a file. The theme is set when the app starts.
 

Attachments

  • ThemeChange.zip
    8.5 KB · Views: 1,030
Last edited:

Similar Threads

  • Article
Android Code Snippet Theme Colors
Replies
3
Views
21K
  • Article
Android Code Snippet Version safe themes
Replies
4
Views
8K
Top