Skip to content

Apply simple renderer to feature layer

View on GitHubSample viewer app

Change the appearance of a feature layer with a renderer.

Image of Apply simple renderer to feature layer sample

Use case

A feature layer hosted on ArcGIS Online has a preset renderer and will display in an application with that renderer. However, for example, the color associated with the original renderer may be unsuitable for a company with staff or clients who are color blind, or for presentation with a different set of basemap and operational layers. In these cases, the renderer on the feature layer's data can be set to a more suitable color.

How to use the sample

Use the button to change the renderer on the feature layer. The original renderer displays orange circles, the diameters of which are proportional to carbon storage of each tree. When the simple renderer is applied, it displays the location of the trees simply as points with random color.

How it works

  1. Create a FeatureLayer from a portal item.
  2. Create a new SimpleRenderer.
  3. Change the feature layer's renderer.

Relevant API

  • FeatureLayer
  • SimpleRenderer

About the data

This sample displays a Landscape Trees feature layer in Warren Wilson College, North Carolina, showing carbon storage of trees on campus. The size of each circle is proportional to that particular tree's carbon storage total.

Tags

feature layer, renderer, visualization

Sample Code

ApplySimpleRendererToFeatureLayerViewModel.ktApplySimpleRendererToFeatureLayerViewModel.ktMainActivity.ktApplySimpleRendererToFeatureLayerScreen.kt
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* 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.applysimplerenderertofeaturelayer.components

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.Color
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.symbology.SimpleMarkerSymbol
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleRenderer
import com.arcgismaps.portal.Portal
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.launch
import kotlin.random.Random

class ApplySimpleRendererToFeatureLayerViewModel(app: Application) : AndroidViewModel(app) {

    // The feature layer from the portal item to which we will apply the renderer
    var featureLayer: FeatureLayer = FeatureLayer.createWithItem(
        PortalItem(
            portal = Portal.arcGISOnline(connection = Portal.Connection.Anonymous),
            itemId = "6d41340931544829acc8f68c27e69dec"
        )
    )

    // The ArcGISMap instance with the basemap and initial viewpoint
    var arcGISMap =
        ArcGISMap(BasemapStyle.ArcGISTopographic).apply {
            initialViewpoint = Viewpoint(latitude = 35.61, longitude = -82.44, scale = 1e4)
            operationalLayers.add(featureLayer)
        }

    // Create a message dialog view model for handling error messages
    val messageDialogVM = MessageDialogViewModel()

    init {
        viewModelScope.launch {
            arcGISMap.load().onFailure { messageDialogVM.showMessageDialog(it) }
        }
    }

    /**
     * Applies a simple renderer with a random color to the feature layer.
     */
    fun applyRandomSimpleRenderer() {
        // Pick a random color from the list
        val colorList = listOf(Color.red, Color.yellow, Color.blue, Color.green)
        val randomColor = colorList[Random.nextInt(colorList.size)]
        // Create a simple marker symbol
        val symbol = SimpleMarkerSymbol(
            style = SimpleMarkerSymbolStyle.Circle,
            color = randomColor,
            size = 10f
        )
        // Create and apply the simple renderer
        val renderer = SimpleRenderer(symbol)
        featureLayer.renderer = renderer
    }
}

// Extension property to provide blue color for ArcGISMaps Color class
val Color.Companion.blue: Color
    get() = fromRgba(0, 0, 255, 255)

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.