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
import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Surfaceimport androidx.compose.runtime.Composableimport com.arcgismaps.ApiKeyimport com.arcgismaps.ArcGISEnvironmentimport com.esri.arcgismaps.sample.sampleslib.theme.SampleAppThemeimport com.esri.arcgismaps.sample.monitorchangestolayerviewstate.screens.MonitorChangesToLayerViewStateScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // authentication with an API key or named user is // required to access basemaps and other location services ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.ACCESS_TOKEN)
setContent { SampleAppTheme { MonitorChangesToLayerViewStateApp() } } }
@Composable private fun MonitorChangesToLayerViewStateApp() { Surface(color = MaterialTheme.colorScheme.background) { MonitorChangesToLayerViewStateScreen( sampleName = getString(R.string.monitor_changes_to_layer_view_state_app_name) ) } }}/* 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.Applicationimport androidx.lifecycle.AndroidViewModelimport androidx.lifecycle.viewModelScopeimport com.arcgismaps.geometry.Pointimport com.arcgismaps.geometry.SpatialReferenceimport com.arcgismaps.mapping.ArcGISMapimport com.arcgismaps.mapping.BasemapStyleimport com.arcgismaps.mapping.PortalItemimport com.arcgismaps.mapping.Viewpointimport com.arcgismaps.mapping.layers.FeatureLayerimport com.arcgismaps.mapping.layers.Layerimport com.arcgismaps.mapping.view.LayerViewStatusimport com.arcgismaps.portal.Portalimport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModelimport kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.asStateFlowimport 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 }}/* 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.screens
import androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.paddingimport androidx.compose.material3.Cardimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Scaffoldimport androidx.compose.material3.Switchimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.runtime.getValueimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.lifecycle.compose.collectAsStateWithLifecycleimport androidx.lifecycle.viewmodel.compose.viewModelimport com.arcgismaps.toolkit.geoviewcompose.MapViewimport com.esri.arcgismaps.sample.monitorchangestolayerviewstate.components.MonitorChangesToLayerViewStateViewModelimport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogimport com.esri.arcgismaps.sample.sampleslib.components.SamplePreviewSurfaceimport com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBar
/** * Main screen layout for the sample app. Observes flows exposed by the ViewModel and * wires MapView callbacks back into the ViewModel. */@Composablefun MonitorChangesToLayerViewStateScreen(sampleName: String) { val mapViewModel: MonitorChangesToLayerViewStateViewModel = viewModel() // Collect flows from the view model val layerIsVisible by mapViewModel.layerIsVisibleFlow.collectAsStateWithLifecycle() val layerStatusLabels by mapViewModel.layerStatusLabelsFlow.collectAsStateWithLifecycle()
Scaffold( topBar = { SampleTopAppBar(title = sampleName) }, content = { padding -> Column( modifier = Modifier .fillMaxSize() .padding(padding) ) { // MapView fills the available space MapView( modifier = Modifier .weight(1f) .fillMaxWidth(), arcGISMap = mapViewModel.arcGISMap, // Listen for layer view state changes and forward to the view model onLayerViewStateChanged = { layerStateChanged -> mapViewModel.onLayerViewStateChanged( layer = layerStateChanged.layer, layerViewStatusList = layerStateChanged.layerViewState.status.toList() ) } ) // Controls are hoisted into a composable function LayerControls( layerIsVisible = layerIsVisible, layerStatusLabels = layerStatusLabels, onToggleVisibility = mapViewModel::updateLayerVisibility ) } // Show message dialog if the view model reported an error mapViewModel.messageDialogVM.apply { if (dialogStatus) { MessageDialog( title = messageTitle, description = messageDescription, onDismissRequest = ::dismissDialog ) } } } )}
@Composableprivate fun LayerControls( layerIsVisible: Boolean, layerStatusLabels: List<String>, onToggleVisibility: (Boolean) -> Unit) { Card(modifier = Modifier.padding(12.dp)) { Column( modifier = Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(12.dp) ) { Row( modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween ) { Text( text = if (layerIsVisible) "Feature layer: Visible" else "Feature layer: Hidden", style = MaterialTheme.typography.titleMedium ) Switch(checked = layerIsVisible, onCheckedChange = onToggleVisibility) }
// Display the current LayerViewState labels Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically ) { Text(text = "Layer view status:", style = MaterialTheme.typography.titleMedium) Text( modifier = Modifier.fillMaxWidth(), text = layerStatusLabels.joinToString(", "), style = MaterialTheme.typography.bodyMedium, textAlign = TextAlign.End ) } } }}
@Preview@Composableprivate fun LayerControlsPreview() { SamplePreviewSurface { LayerControls( layerIsVisible = true, layerStatusLabels = listOf("Active"), onToggleVisibility = { } ) }}