The Theme API provides the following classes and interfaces (packaged as a part of the Nokia API):
ThemeManager allows for registering ThemeChangeListeners, getting a list of themes, getting the current active theme and switching to a different theme.
Theme contains methods and fields that you can use to get information about the native theme.
ThemeChangeListener enables an application to listen for a theme change events.
For an example MIDlet that demonstrates how to use the Theme API, see Theme API - ThemeTest.
You can use the getThemes() method of the ThemeManager class to get the list of all the available themes.
The following code demonstrates how to use the getThemes() method and get the list of all the available themes:
try { Theme[] themes = ThemeManager.getThemes(); for (int i = 0; i < themes.length; i++) { themes[i].getName(); } } catch (ThemeException e) { // generic error }
The available themes may vary between different products.
You can use the getCurrentTheme() method of the ThemeManager class to get the current active theme.
The following code demonstrates how to use the getCurrentTheme() method to get the current active theme:
try { Theme theme = ThemeManager.getCurrentTheme(); } catch (ThemeException e) { // generic error }
You can load a color from the current active theme using the getColor() method as shown in the following code:
int colorId = Theme.COLOR_FOREGROUND; try { Theme theme = ThemeManager.getCurrentTheme(); int color = theme.getColor(colorId); } catch (ThemeException e) { // generic error } catch (IllegalArgumentException e) // color-id is invalid }
You can use the setCurrentTheme() method of the ThemeManager class to set a specific theme for your MIDlet.
The following code demonstrates how to use the setCurrentTheme() method to set a specific theme for your MIDlet:
try { ThemeManager.setCurrentTheme(theme); } catch (ThemeException e) { // generic error }
You can switch to a specific theme by name by iterating the list of available themes and finding the correct theme you need, as shown in the following code:
String name = "dark"; try { Theme[] themes = ThemeManager.getThemes(); for (int i = 0; i < themes.length; i++) { if (name.equals(themes[i].getName())) { ThemeManager.setCurrentTheme(themes[i]); break; } } } catch (ThemeException e) { // generic error }
You can use the getImage() method of the Theme interface to load an image from a specific theme.
The following code demonstrates how to use the getImage() method to load an image from a specific theme:
int imageId = Theme.IMAGE_SMILEY; try { Theme theme = ThemeManager.getCurrentTheme(); Image image = theme.getImage(imageId); if (image != null) { // success } else { // failed } } catch (ThemeException e) { // generic error } catch (IllegalArgumentException e) { // theme-id is invalid } catch (SecurityException e) { // MIDlet isn't signed properly }
Only operator and manufacturer signed MIDlets can load images from themes.
You can use the addThemeChangeListener() method of the ThemeManager class to listen for theme changes.
The call to ThemeChangeListener.themeChanged(Theme) MUST return immediately. Any heavy-lifting needs to be done on a separate thread to avoid blocking the Event thread (which would block the UI). The following code demonstrates how this is done and how to use the addThemeChangeListener() method to listen for changes in a specific theme:
public class ThemeChangeHandler extends Thread implements ThemeChangeListener { private boolean running = false; private Theme theme; public void themeChanged(Theme newTheme) { theme = newTheme; synchronized (lock) { lock.notify(); } } public void requestStop() { running = false; synchronized (lock) { lock.notify(); } } public void run() { running = true; try { while (running) { synchronized(lock) { lock.wait(); } if (running) { // do theme change actions } } } catch (InterruptedException e) { } } } public void startApp() { ThemeChangeHandler handler = new ThemeChangeHandler(); handler.start(); try { ThemeManager.addThemeChangeListener(handler); } catch (ThemeException e) { // generic error } } public void destroyApp(boolean unconditional) { ThemeManager.removeThemeChangeListener(handler); handler.requestStop(); try { handler.join(); } catch (InterruptedException e) { } }
Theme changes can currently only be initiated by the MIDlet.