Determine whether a layer is currently visible.
Use case
The view status includes information on the loading state of layers and whether layers are visible at a given scale. You might change how a layer is displayed in a layer list to communicate whether it is being viewed on the map. For example, you could show a loading spinner next to its name when the view status is Loading, gray out the name when notVisible or OutOfScale, show the name normally when Active, or with a warning or error icon when the status is Warning or Error.
How to use the sample
When the feature layer is loaded, pan and zoom around the map. Note how the LayerViewState flags change. For example, the layer status OutOfScale is retrieved when the map is scaled outside the layer's min and max scale range. Tap the toggle to hide the layer and observe the view state change to NotVisible. Disconnect from the network and pan around the map to see the Warning status when the layer cannot fetch online data. Reconnect to the network to see the warning disappear.
How it works
- Create a
ArcGISMapwith an operational layer. - Use the
onLayerViewStateChanged()callback on the map view to get updates to the layers' view states. - Get the
Layerand the current viewstatusof theLayerViewStatedefining the new state.
Relevant API
- GeoViewLayerViewStateChanged
- Layer
- LayerViewState
- Map
- MapView
About the data
The Satellite (MODIS) Thermal Hotspots and Fire Activity layer presents detectable thermal activity from MODIS satellites for the last 48 hours. MODIS Global Fires is a product of NASA’s Earth Observing System Data and Information System (EOSDIS), part of NASA's Earth Science Data. EOSDIS integrates remote sensing and GIS technologies to deliver global MODIS hotspot/fire locations to natural resource managers and other stakeholders around the World.
Additional information
The following are the LayerViewState.status options:
Active: The layer in the view is active.NotVisible: The layer in the view is not visible.OutOfScale: The layer in the view is out of scale. A status ofOutOfScaleindicates that the view is zoomed outside of the scale range of the layer. If the view is zoomed too far in (e.g., to a street level), it is beyond the max scale defined for the layer. If the view has zoomed too far out (e.g., to global scale), it is beyond the min scale defined for the layer.Loading: The layer in the view is loading. Once loading has completed, the layer will be available for display in the view. If there was a problem loading the layer, the status will be set toError.Error: The layer in the view has an unrecoverable error. When the status isError, the layer cannot be rendered in the view. For example, it may have failed to load, be an unsupported layer type, or contain invalid data.Warning: The layer in the view has a non-breaking problem with its display, such as incomplete information (e.g., by requesting more features than the max feature count of a service) or a network request failure.
Tags
layer, load, map, status, view, visibility
Sample Code
/* Copyright 2025 Esri
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.esri.arcgismaps.sample.monitorchangestolayerviewstate.components
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.SpatialReference
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.PortalItem
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.layers.FeatureLayer
import com.arcgismaps.mapping.layers.Layer
import com.arcgismaps.mapping.view.LayerViewStatus
import com.arcgismaps.portal.Portal
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
/**
* ViewModel that creates the map and feature layer, observes load state and exposes
* simple state flows for UI to display layer visibility and the current layer view status.
*/
class MonitorChangesToLayerViewStateViewModel(app: Application) : AndroidViewModel(app) {
// The Satellite (MODIS) Thermal Hotspots and Fire Activity portal item with a feature layer.
private val featureLayer: FeatureLayer by lazy {
val portalItem = PortalItem(
portal = Portal("https://www.arcgis.com"),
itemId = "b8f4033069f141729ffb298b7418b653"
)
FeatureLayer.createWithItem(item = portalItem).apply {
minScale = 1e8
maxScale = 6e6
}
}
// Map with Topographic basemap and initial viewpoint
val arcGISMap: ArcGISMap = ArcGISMap(BasemapStyle.ArcGISTopographic).apply {
initialViewpoint = Viewpoint(
center = Point(x = -13e6, y = 51e5, spatialReference = SpatialReference.webMercator()),
scale = 2e7
)
// Add the feature layer to the map's operational layers
operationalLayers.add(featureLayer)
}
// Expose whether the layer is visible, UI can trigger flow in updateLayerVisibility().
private val _layerIsVisibleFlow = MutableStateFlow(true)
val layerIsVisibleFlow = _layerIsVisibleFlow.asStateFlow()
// Expose the current LayerViewStatus simple names for the UI
private val _layerStatusLabelsFlow = MutableStateFlow(listOf("N/A"))
val layerStatusLabelsFlow = _layerStatusLabelsFlow.asStateFlow()
// Message dialog for displaying errors
val messageDialogVM = MessageDialogViewModel()
init {
viewModelScope.launch {
// Load the map which contains the configured feature layer
arcGISMap.load().onFailure { error ->
messageDialogVM.showMessageDialog(error)
}
}
}
/**
* Called from the MapView's onLayerViewStateChanged callback.
*/
fun onLayerViewStateChanged(
layer: Layer,
layerViewStatusList: List<LayerViewStatus>
) {
// Only observe state of the added feature layer.
if (layer.id != featureLayer.id) return
// Convert the current layer view status list
// to a list of plain strings using ::class.java.simpleName
_layerStatusLabelsFlow.value = if (layerViewStatusList.isNotEmpty()) {
layerViewStatusList.map { it::class.java.simpleName }
} else {
listOf("N/A")
}
}
/**
* Toggle visibility of the sample feature layer and update flow state.
*/
fun updateLayerVisibility(visible: Boolean) {
_layerIsVisibleFlow.value = visible
featureLayer.isVisible = visible
}
}