Show status messages

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.

mapbox_activity_show_status.xml
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.maps.MapView
android:id="@+id/mapView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:background="#00ccff" />
<com.mapbox.navigation.ui.status.view.MapboxStatusView
android:id="@+id/statusView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ShowStatusActivity.kt
package com.mapbox.navigation.examples.status
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.maps.Style
import com.mapbox.maps.plugin.gestures.OnMapClickListener
import com.mapbox.maps.plugin.gestures.gestures
import com.mapbox.maps.plugin.scalebar.scalebar
import com.mapbox.navigation.base.ExperimentalMapboxNavigationAPI
import com.mapbox.navigation.examples.R
import com.mapbox.navigation.examples.databinding.MapboxActivityShowStatusBinding
import com.mapbox.navigation.ui.status.model.Status
import com.mapbox.navigation.ui.status.model.StatusFactory
import com.mapbox.navigation.ui.status.view.MapboxStatusView
/**
* In this example you can learn how to use [MapboxStatusView] to show a [Status] message
* after tapping anywhere on the map.
*/
@OptIn(ExperimentalMapboxNavigationAPI::class)
class ShowStatusActivity : AppCompatActivity() {
private lateinit var binding: MapboxActivityShowStatusBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = MapboxActivityShowStatusBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.mapView.apply {
scalebar.enabled = false
gestures.addOnMapClickListener(onMapClickListener)
getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS)
}
}
private fun showStatusMessage() {
val status = StatusFactory.buildStatus(
message = "Voice instructions OFF",
duration = 2000,
icon = R.drawable.mapbox_ic_sound_off
)
binding.statusView.render(status)
}
private val onMapClickListener = OnMapClickListener {
// show status message on map click
showStatusMessage()
false
}
}