View on GitHub Sample viewer app

Display custom labels in a 3D scene.

Image of show labels on layer in 3D

Use case

Labeling features is useful to visually display a key piece of information or attribute of a feature on a map. For example, you may want to label rivers or streets with their names.

How to use the sample

Pan and zoom to explore the scene. Notice the labels showing installation dates of features in the 3D gas network.

How it works

  1. Create an ArcGISScene from a PortalItem.
  2. Add the scene to an SceneViewand load it.
  3. After loading is complete, obtain the FeatureLayer from one of the GroupLayers in the scene’s operationalLayers.
  4. Set the feature layer’s labelsEnabled property to true.
  5. Create an TextSymbol to use for displaying the label text.
  6. Create an LabelDefinition using an ArcadeLabelExpression.
  7. Add the definition to the feature layer’s labelDefinitions array.

Relevant API

  • ArcadeLabelExpression
  • FeatureLayer
  • LabelDefinition
  • Scene
  • SceneView
  • TextSymbol

About the data

This sample shows a New York City infrastructure scene hosted on ArcGIS Online.

Additional information

Help regarding the Arcade label expression script for defining a label definition can be found on the ArcGIS Developers site.

Tags

3D, arcade, attribute, buildings, label, model, scene, symbol, text, URL, visualization

Sample code

ShowLabelsOnLayerIn3DViewModel.ktShowLabelsOnLayerIn3DViewModel.ktMainActivity.ktShowLabelsOnLayerIn3DScreen.kt
/* Copyright 2026 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.showlabelsonlayerin3d.components
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.Color
import com.arcgismaps.arcade.ArcadeExpression
import com.arcgismaps.arcgisservices.LabelingPlacement
import com.arcgismaps.mapping.ArcGISScene
import com.arcgismaps.mapping.PortalItem
import com.arcgismaps.mapping.labeling.ArcadeLabelExpression
import com.arcgismaps.mapping.labeling.LabelDefinition
import com.arcgismaps.mapping.layers.FeatureLayer
import com.arcgismaps.mapping.layers.GroupLayer
import com.arcgismaps.mapping.symbology.TextSymbol
import com.arcgismaps.portal.Portal
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.launch
class ShowLabelsOnLayerIn3DViewModel(app: Application) : AndroidViewModel(app) {
val arcGISScene = ArcGISScene(
item = PortalItem(
portal = Portal.arcGISOnline(Portal.Connection.Anonymous),
itemId = "850dfee7d30f4d9da0ebca34a533c169"
)
)
// Create the text symbol for the label definition.
private val textSymbol = TextSymbol().apply {
color = Color.red
haloColor = Color.white
haloWidth = 2.0f
size = 16.0f
}
// Create the label definition to display the installation date of a feature.
private val labelDefinition = LabelDefinition(
labelExpression = ArcadeLabelExpression(
arcadeExpression = ArcadeExpression(
expression = $$"Text($feature.INSTALLATIONDATE, `DD MMM YY`)"
)
),
textSymbol = textSymbol
).apply {
placement = LabelingPlacement.LineAboveAlong
useCodedValues = true
}
init {
viewModelScope.launch {
// Load the scene
arcGISScene.load().onFailure { messageDialogVM.showMessageDialog(it) }
// Find the group layer and feature layer
val groupLayer = arcGISScene.operationalLayers
.filterIsInstance<GroupLayer>()
.first { it.name == "Gas" }
val featureLayer = groupLayer.layers
.filterIsInstance<FeatureLayer>()
.first { it.name == "Gas Main" }
// Enable labels and add the label definition to the feature layer
featureLayer.apply {
labelsEnabled = true
labelDefinitions.clear()
labelDefinitions.add(labelDefinition)
}
}
}
// Create a message dialog view model for handling error messages
val messageDialogVM = MessageDialogViewModel()
}