• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Toggle Navigation Theme

Switch Navigation Theme Mode

This example shows:

  • How to config custom navigation style using NavLauncherConfig.Builder

  • How to switch Navigation Theme Mode between Light, Dark, and System using configBuilder.themeMode(navigationThemeMode)

    • NavigationConstants.NAVIGATION_VIEW_LIGHT_MODE

    • NavigationConstants.NAVIGATION_VIEW_DARK_MODE

    • NavigationConstants.NAVIGATION_VIEW_FOLLOW_SYSTEM_MODE

For all code examples, refer to Navigation Code Examples

activity_toggle_navigation_theme.xml view source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/fetchRoute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/fetch_route"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            android:id="@+id/startNavLight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:text="@string/start_navigation_with_light_mode"/>
        <Button
            android:id="@+id/startNavDark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:text="@string/start_navigation_with_dark_mode"/>
        <Button
            android:id="@+id/startNavAsSetting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:text="@string/start_navigation_with_system_setting_mode"/>
    </LinearLayout>

    <TextView
        android:id="@+id/routeGeometry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"/>

    <ProgressBar
        android:id="@+id/progress"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

</LinearLayout>

themes.xml view source

1
2
3
4
5
6
7
8
<style name="Theme.CustomNavigationLight" parent="NavigationViewLight">
        <!-- You can customise your navigation style here-->
    </style>


    <style name="Theme.CustomNavigationDark" parent="NavigationViewDark">
        <!-- You can customise your navigation style here-->
    </style>

ToggleNavigationThemeActivity view source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ai.nextbillion.navigation.demo.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import ai.nextbillion.kits.directions.models.DirectionsResponse;
import ai.nextbillion.kits.directions.models.DirectionsRoute;
import ai.nextbillion.kits.geojson.Point;
import ai.nextbillion.maps.location.modes.RenderMode;
import ai.nextbillion.navigation.core.navigation.NavigationConstants;
import ai.nextbillion.navigation.demo.R;
import ai.nextbillion.navigation.ui.NBNavigation;
import ai.nextbillion.navigation.ui.NavLauncherConfig;
import ai.nextbillion.navigation.ui.NavigationLauncher;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ToggleNavigationThemeActivity extends AppCompatActivity implements View.OnClickListener {


    private Button fetchRoute;
    private Button startNavLight;
    private Button startNavDark;
    private Button startNavAsSetting;
    private TextView routeGeometry;
    private DirectionsRoute directionsRoute;
    private ProgressBar progress;
    private String navigationThemeMode = NavigationConstants.NAVIGATION_VIEW_LIGHT_MODE;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toggle_navigation_theme);
        fetchRoute = findViewById(R.id.fetchRoute);
        startNavLight = findViewById(R.id.startNavLight);
        startNavDark = findViewById(R.id.startNavDark);
        startNavAsSetting = findViewById(R.id.startNavAsSetting);
        routeGeometry = findViewById(R.id.routeGeometry);
        progress = findViewById(R.id.progress);
        fetchRoute.setOnClickListener(this);
        startNavLight.setOnClickListener(this);
        startNavDark.setOnClickListener(this);
        startNavAsSetting.setOnClickListener(this);

        startNavLight.setEnabled(false);
        startNavDark.setEnabled(false);
        startNavAsSetting.setEnabled(false);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.fetchRoute) {
            progress.setVisibility(View.VISIBLE);
            Point origin = Point.fromLngLat(103.75986708439264, 1.312533169133601);
            Point destination = Point.fromLngLat(103.77982271935586, 1.310473772283314);

            NBNavigation.fetchRoute(origin, destination, new Callback<DirectionsResponse>() {
                @Override
                public void onResponse(@NonNull Call<DirectionsResponse> call, @NonNull Response<DirectionsResponse> response) {
                    progress.setVisibility(View.GONE);
                    //start navigation with the route we just fetched.
                    if (response.body() != null && !response.body().routes().isEmpty()) {
                        directionsRoute = response.body().routes().get(0);
                        routeGeometry.setText(String.format("Route Geometry: %s", directionsRoute.geometry()));
                        startNavLight.setEnabled(true);
                        startNavDark.setEnabled(true);
                        startNavAsSetting.setEnabled(true);                    }
                }

                @Override
                public void onFailure(@NonNull Call<DirectionsResponse> call, @NonNull Throwable t) {
                    progress.setVisibility(View.GONE);
                }
            });
        } else if (view.getId() == R.id.startNavLight) {
            navigationThemeMode = NavigationConstants.NAVIGATION_VIEW_LIGHT_MODE;
            fetchRoute();
        } else if (view.getId() == R.id.startNavDark) {
            navigationThemeMode = NavigationConstants.NAVIGATION_VIEW_DARK_MODE;
            fetchRoute();
        }  else if (view.getId() == R.id.startNavAsSetting) {
            navigationThemeMode = NavigationConstants.NAVIGATION_VIEW_FOLLOW_SYSTEM_MODE;
            fetchRoute();
        }
    }

    private void fetchRoute() {
        NavLauncherConfig.Builder configBuilder = NavLauncherConfig.builder(directionsRoute);
        configBuilder.locationLayerRenderMode(RenderMode.GPS);
        configBuilder.shouldSimulateRoute(true);
        configBuilder.themeMode(navigationThemeMode);
//        You can customise your Navigation Light theme and Navigation Dark theme
//        configBuilder.lightThemeResId(R.style.Theme_CustomNavigationLight);
//        configBuilder.darkThemeResId(R.style.Theme_CustomNavigationDark);

        NavigationLauncher.startNavigation(ToggleNavigationThemeActivity.this, configBuilder.build());
    }


}

Code Highlights

This code example defines an activity that allows the user to toggle between different navigation themes and start navigation based on a fetched route.

The Code defined Theme_CustomNavigationLight based on NavigationViewLight and Theme_CustomNavigationDark based on NavigationViewDark These styles are used to customize the appearance of the navigation view in Light mode and Dark mode.

Code summary

In the code example, configBuilder.themeMode(NavigationConstants.NAVIGATION_VIEW_LIGHT_MODE) method is used to set the theme mode for the navigation interface in the NavLauncherConfig object. The theme mode determines the visual style of the navigation UI. The method accepts a string parameter representing the theme mode. Defaults to NAVIGATION_VIEW_FOLLOW_SYSTEM_MODE

  • NAVIGATION_VIEW_LIGHT_MODE: This theme mode sets the navigation interface to use a light color scheme. It is suitable for environments with a bright background or during daytime. It provides a visually lighter and brighter appearance.

  • NAVIGATION_VIEW_DARK_MODE: This theme mode sets the navigation interface to use a dark color scheme. It is suitable for environments with low light conditions or during nighttime. It provides a visually darker and more contrasted appearance.

  • NAVIGATION_VIEW_FOLLOW_SYSTEM_MODE: This theme mode allows the navigation interface to follow the system-wide theme settings. It adapts to the user's preferred system theme, whether it is light or dark. The interface will automatically switch between light and dark modes based on the system's current theme settings.

By using these theme modes, you can customize the visual style of the navigation UI to match your application's design or provide a consistent experience with the system-wide theme.

configBuilder.lightThemeResId(R.style.Theme_CustomNavigationLight) sets the light theme for the navigation UI, using the Theme_CustomNavigationLight style defined earlier. This style determines the appearance of the navigation view in light mode.

configBuilder.darkThemeResId(R.style.Theme_CustomNavigationDark) sets the dark theme for the navigation UI, using the Theme_CustomNavigationLight style defined earlier. This style is applied when the device's dark mode is enabled.