Dynamic, Light and Dark Theme
  • 05 Sep 2024
  • 2 Minutes to read
  • Contributors
  • Dark
    Light

Dynamic, Light and Dark Theme

  • Dark
    Light

Article summary

The Vuzix Blade 2 has three display modes (Dynamic, Light and Dark modes). The display modes provide an optimal display experience in different ambient lighting conditions.

Those styles are automatically changed by the Blade 2 OS based on the front-facing sensors.

The developer can take advantage of this feature and use HUD resources to allow the application Theme to change.

It can also be used to Allow other parts of your application to dynamically adjust as well.

The com.vuzix.hud.resources class defines two styles. The two theme styles: HudTheme and HudTheme.Light.

This corresponds to the two display modes of the Vuzix Blade 2.

Note: The com.vuzix.hud.actionmenu classes have a dependency on com.vuzix.hud.resources.

Thus, since com.vuzix.hud.actionmenu classes are in the dependencies section of the build.gradle file, Android Studio also downloads the resources classes.


Extend HUDTheme Styles

A styles.xml (res/values.styles.xml) file will be created by Android Studio.

You will need to modify the file to have two separate themes that reference HUD resource main themes.

Create an AppTheme that extends the HudTheme from com.vuzix.hud.resources and also create a AppTheme.Light that extends the HudTheme.Light from the same package.

For more details, you can read the JavaDocs.

<style name="AppTheme" parent="HudTheme">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Light" parent="HudTheme.Light">
<!-- Customize your theme here. -->
</style>

styles.xml


DynamicThemeApplication Class

Extend the com.vuzix.hud.resources.DynamicThemeApplication class to create an application that will dynamically change themes depending on the current display mode.

If you utilize this feature, the application will automatically receive a notification from the system and handle the theme change if correctly configured.

In the example application, a new Blade2SampleApplication class is created which extends com.vuzix.hud.resources.DynamicThemApplication.

Note: Be sure to set the Superclass as com.vuzix.hud.resources.DynamicThemeApplication.

Once the Blade2SampleApplication class is generated by Android Studio, two methods must be overridden.

The methods that will need to be extended are getNormalThemeResId and getLightThemeResId. These two classes return the new styles defined in the styles.xml file modified above.

Java

@Override protected int getNormalThemeResId() {
	return R.style.AppTheme;
}
@Override protected int getLightThemeResId() {
	return R.style.AppTheme_Light;
}


Blade2SampleApplication Class

Next we have to register the new application in the AndroidManifest.xml file.

This is shown on line 6 of the screen below.

Java

android:name=".Blade2SampleApplication"

Application defined in AndroidManifest.xml

Now that your application is created and registered, your application will automatically change based on the Blade 2 OS understanding of ambient light conditions.


Dynamic Themes Broadcast

Blade 2 OS will send a broadcast with the following action:

  • com.vuzix.intent.action.UI_DISPLAY_MODE

This broadcast can also be received manually to perform any desire actions.

To receive and modify this broadcast you can just follow the normal Android OS Broadcast development work.

We will create a custom broadcast receiver to intercept that message. Once we have that message, we can use it to do custom actions or signal other parts of the application to update.

First we create a new Java class (File > New > Other > Broadcast Receiver) and provide the following information:

  • Name: Name of Class

New Android Component

Once the file gets generated, the new class will open up and have a TODO of implement method.

For now we can delete all the code inside the method and leave it blank.

You can use this method to override and do custom work desired when the Blade 2 OS signals for a dynamic theme change request.

Now we go to the Android manifest and tell the system which broadcast this receiver should receive.

We do this by adding an Intent Filter for : "com.vuzix.intent.action.UI_DISPLAY_MODE"

Java

<intent-filter>
	<action android:name="com.vuzix.intent.action.UI_DISPLAY_MODE" />
</intent-filter>

You now have a broadcast receiver to intercept and perform custom work on a dynamic theme change request.

Now lets continue the sample project by creating and usingActionMenuActivity and ActionMenu.


Was this article helpful?

What's Next