Change Navigation View Options at runtime
Note
This example is a part of the Navigation SDK Examples. You can find the values for all referenced resources in the res
directory. For example, see res/values/strings.xml
for R.string.*
references used in this example.
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"> <com.mapbox.navigation.dropin.NavigationViewandroid:id="@+id/navigationView"android:layout_width="match_parent"android:layout_height="match_parent"app:accessToken="@string/mapbox_access_token" /> <com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:id="@+id/toggleOptions"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="16dp"android:layout_marginBottom="120dp"android:src="@drawable/mapbox_ic_customize"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintBottom_toBottomOf="parent"tools:ignore="ContentDescription"/></androidx.constraintlayout.widget.ConstraintLayout>
package com.mapbox.navigation.examples.preview.dropinui import android.graphics.Colorimport android.os.Bundleimport androidx.appcompat.app.AppCompatActivityimport com.mapbox.maps.Styleimport com.mapbox.navigation.dropin.ViewOptionsCustomizationimport com.mapbox.navigation.dropin.ViewOptionsCustomization.Companion.defaultRouteArrowOptionsimport com.mapbox.navigation.dropin.ViewOptionsCustomization.Companion.defaultRouteLineOptionsimport com.mapbox.navigation.examples.preview.databinding.MapboxActivityCustomizeNavigationviewOptionsBindingimport com.mapbox.navigation.ui.maps.NavigationStylesimport com.mapbox.navigation.ui.maps.route.RouteLayerConstantsimport com.mapbox.navigation.ui.maps.route.arrow.model.RouteArrowOptionsimport com.mapbox.navigation.ui.maps.route.line.model.MapboxRouteLineOptionsimport com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResourcesimport com.mapbox.navigation.ui.maps.route.line.model.RouteLineResources /*** The example demonstrates how to use [ViewOptionsCustomization] to customize the options for* selective UI components at runtime.** Before running the example make sure you have put your access_token in the correct place* inside [app-preview/src/main/res/values/mapbox_access_token.xml]. If not present then add* this file at the location mentioned above and add the following content to it** <?xml version="1.0" encoding="utf-8"?>* <resources xmlns:tools="http://schemas.android.com/tools">* <string name="mapbox_access_token"><PUT_YOUR_ACCESS_TOKEN_HERE></string>* </resources>** The example uses replay location engine to facilitate navigation without physically moving.** How to use the example:* - Start the example* - Grant the location permissions if not already granted* - Press on the floating button in the bottom right to apply custom options*/class CustomNavigationViewOptionsActivity : AppCompatActivity() { private lateinit var binding: MapboxActivityCustomizeNavigationviewOptionsBindingprivate var toggleCustomOptions = false private val routeLineOptions: MapboxRouteLineOptions by lazy {MapboxRouteLineOptions.Builder(this).withRouteLineResources(RouteLineResources.Builder().routeLineColorResources(RouteLineColorResources.Builder().routeLowCongestionColor(Color.YELLOW).routeCasingColor(Color.RED).build()).build())// make sure to use the correct layerId based on the map styles.withRouteLineBelowLayerId("road-label") // for Style.LIGHT and Style.DARK.withVanishingRouteLineEnabled(true).displaySoftGradientForTraffic(true).build()} private val routeArrowOptions by lazy {RouteArrowOptions.Builder(this).withAboveLayerId(RouteLayerConstants.TOP_LEVEL_ROUTE_LINE_LAYER_ID).withArrowColor(Color.RED).build()} override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = MapboxActivityCustomizeNavigationviewOptionsBinding.inflate(layoutInflater)setContentView(binding.root) binding.navigationView.api.routeReplayEnabled(true) binding.toggleOptions.setOnClickListener {binding.navigationView.customizeViewOptions {toggleCustomOptions = if (toggleCustomOptions) {routeLineOptions = defaultRouteLineOptions(applicationContext)routeArrowOptions = defaultRouteArrowOptions(applicationContext)mapStyleUriDay = NavigationStyles.NAVIGATION_DAY_STYLEmapStyleUriNight = NavigationStyles.NAVIGATION_NIGHT_STYLEfalse} else {routeLineOptions = this@CustomNavigationViewOptionsActivity.routeLineOptionsrouteArrowOptions = this@CustomNavigationViewOptionsActivity.routeArrowOptionsmapStyleUriDay = Style.LIGHTmapStyleUriNight = Style.DARKtrue}}}}}