Add a feature layer from a portal item

Learn how to use an ArcGIS portal item to access and display a feature layer in a map.

add a feature layer from a portal item

You can host a variety of geographic data and other resources using ArcGIS Online. These portal items can also define how the data is presented. A web map or web scene, for example, not only defines the layers for a map or scene, but also how layers are symbolized, the minimum and/or maximum scales at which they display, and several other properties. Likewise, a hosted feature layer contains the data for the layer and also defines the symbols and other display properties for how it is presented. When you add a map, scene, or layer from a portal item to your ArcGIS Runtime app, everything that has been saved with the item is applied in your app. Adding portal items to your ArcGIS Runtime app rather than creating them programmatically saves you from writing a lot of code, and can provide consistency across apps that use the same data.

In this tutorial, you will add a hosted feature layer to display trailheads in the Santa Monica Mountains of Southern California. The hosted layer defines the trailhead locations (points) as well as the symbols used to display them.

Prerequisites

The following are required for this tutorial:

  1. An ArcGIS account to access API keys. If you don't have an account, sign up for free.
  2. Confirm that your system meets the system requirements.
  3. An IDE for Android development in Kotlin.

Steps

Open an Android Studio project with Gradle

  1. To start this tutorial, complete the Display a map tutorial. Or download and unzip the Display a map solution in a new folder.

  2. Modify the old project for use in this new tutorial. Expand More info for instructions.

    1. On your file system, delete the .idea folder, if present, at the top level of your project.

    2. In the Android tool window, open app > res > values > strings.xml.

      In the <string name="app_name"> element, change the text content to Add a feature layer from a portal item.

      strings.xml
      Use dark colors for code blocks
      1 2 3 4 5
      Change line
      1
      2
      3
      4
      5
      <resources>
      
          <string name="app_name">Add a feature layer from a portal item</string>
      
      </resources>
    3. In the Android tool window, open Gradle Scripts > settings.gradle.

      Change the value of rootProject.name to "Add a feature layer from a portal item".

      settings.gradle
      Expand
      Use dark colors for code blocks
      21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 22 23 24
      Change line
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      rootProject.name = "Add a feature layer from a portal item"
      
      include ':app'
    4. Click File > Sync Project with Gradle files. Android Studio will recognize your changes and create a new .idea folder.

  3. If you downloaded the Display a map solution project, get your API key and set it in your app.

    An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.

    1. Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.

    2. In Android Studio: in the Android tool window, open app > java > com.example.app > MainActivity.

    3. In the setApiKeyForApp() method, find the ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") call and paste your API key inside the quotes, replacing YOUR_API_KEY.

      MainActivity.kt
      Use dark colors for code blocks
                                                                                               
      Change line
      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
      84
      85
      86
      87
      88
      89
          override fun onDestroy() {
              mapView.dispose()
              super.onDestroy()
          }
      
          private fun setApiKeyForApp(){
              // set your API key
              // Note: it is not best practice to store API keys in source code. The API key is referenced
              // here for the convenience of this tutorial.
      
              ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")
      
          }
      

Prepare files before coding the app

Modify the files from the Display a map tutorial so they can be used in this tutorial: you will add imports and remove code that we will not need.

  1. In the Project tool window, make sure that the Android view is displayed. Open app/java/com.example.app, and click MainActivity.kt. Add the following imports, replacing those from the Display a map tutorial.

    MainActivity.kt
    Use dark colors for code blocks
                                                                                                                 
    Change lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange line
    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    package com.example.app
    
    import android.os.Bundle
    import android.util.Log
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
    import com.esri.arcgisruntime.layers.FeatureLayer
    import com.esri.arcgisruntime.loadable.LoadStatus
    import com.esri.arcgisruntime.mapping.ArcGISMap
    import com.esri.arcgisruntime.mapping.BasemapStyle
    import com.esri.arcgisruntime.mapping.Viewpoint
    import com.esri.arcgisruntime.mapping.view.MapView
    import com.esri.arcgisruntime.portal.Portal
    import com.esri.arcgisruntime.portal.PortalItem
    
    import com.example.app.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
  2. Delete the code for creating an ArcGISMap and setting the map on the map view. In the next section, we will code these steps in a different way, using a Kotlin scope function.

    The only code left in setupMap at this point should be the line that sets the view point.

    MainActivity.kt
    Use dark colors for code blocks
                                                                                             
    Remove lineRemove lineRemove lineRemove line
    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
    84
    85
    86
    87
    88
    89
        // set up your map here. You will call this method from onCreate()
        private fun setupMap() {
    
            // create a map with the BasemapStyle streets
            val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC)
    
            // set the map to be displayed in the layout's MapView
            mapView.map = map
    
            // set the viewpoint, Viewpoint(latitude, longitude, scale)
            mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0))
    
        }
    
  3. Create a companion object containing the private read-only value named TAG, which will be used by the Toast when displaying any portal item loading error.

    MainActivity.kt
    Use dark colors for code blocks
                                                                                                                 
    Add line.Add line.Add line.
    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    class MainActivity : AppCompatActivity() {
    
        companion object {
            private val TAG: String = MainActivity::class.java.simpleName
        }
    
        private val activityMainBinding by lazy {
          ActivityMainBinding.inflate(layoutInflater)
        }
    
        private val mapView: MapView by lazy {
          activityMainBinding.mapView
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContentView(activityMainBinding.root)
            setApiKeyForApp()
            setupMap()
        }
    

Display the ArcGIS portal item

You can reference an item (such as a web map or feature layer) hosted in a portal (such as ArcGIS Online) using its unique item ID. You will reference the Trailheads Styled feature layer stored in ArcGIS Online using its item ID: 2e4b3df6ba4b44969a3bc9827de746b3. You will then add that feature layer to your map's collection of data layers (operational layers).

  1. In the setupMap() function, directly before the mapView.setViewpoint() call, do the following: create a PortalItem object that references the portal item that the feature layer will use. To do this, provide the item ID and an Portal object.

    MainActivity.kt
    Use dark colors for code blocks
                                                                                                                 
    Add line.Add line.Add line.
    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
        // set up your map here. You will call this method from onCreate()
        private fun setupMap() {
    
            val portalItemId = "2e4b3df6ba4b44969a3bc9827de746b3"
            val portal = Portal("https://www.arcgis.com")
            val portalItem = PortalItem(portal, portalItemId)
    
            mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0))
    
        }
    
  2. Create a FeatureLayer passing the PortalItem and a layer ID of 0. Since the layerId parameter to the FeatureLayer constructor is a long, you can use the Kotlin function toLong() to convert the layer ID integer to a Long. Using a PortalItem to create a FeatureLayer causes the portal item to be loaded asynchronously.

    A layer ID is required because a portal item may have more than one layer. For instance, when a feature service has 3 layers, such as trailheads (points), trails (polylines) and trail areas (polygons), the corresponding portal item would contain all three layers.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 82 83 84 85 86 87 88 89 90 91 91 91 91 91 92 93 93 93 93 93 93 93 93 93 93 94 94 94
    Add line.Add line.
    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
        // set up your map here. You will call this method from onCreate()
        private fun setupMap() {
    
            val portalItemId = "2e4b3df6ba4b44969a3bc9827de746b3"
            val portal = Portal("https://www.arcgis.com")
            val portalItem = PortalItem(portal, portalItemId)
    
            val layerId = 0
            val layer = FeatureLayer(portalItem, layerId.toLong())
    
            mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0))
    
        }
    
    Expand
  3. Create a ArcGISMap with a BasemapStyle named ARCGIS_TOPOGRAPHIC. Next, configure the map using the Kotlin scope function apply(). In the lambda that you pass to apply(), get the map's operational layers collection and add the layer to the collection.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 97 97 97 97 97 97 97 97 97 98 98 98
    Add line.Add line.Add line.
    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
        // set up your map here. You will call this method from onCreate()
        private fun setupMap() {
    
            val portalItemId = "2e4b3df6ba4b44969a3bc9827de746b3"
            val portal = Portal("https://www.arcgis.com")
            val portalItem = PortalItem(portal, portalItemId)
    
            val layerId = 0
            val layer = FeatureLayer(portalItem, layerId.toLong())
    
            mapView.map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC).apply {
                operationalLayers.add(layer)
            }
    
            mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0))
    
        }
    
    Expand
  4. Leave the mapView.setViewpoint() line unchanged (in blue highlight below).

    MainActivity.kt
    Expand
    Use dark colors for code blocksCopy
    81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 97 97 97 97 97 97 97 97 97 98 98 98
    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
        // set up your map here. You will call this method from onCreate()
        private fun setupMap() {
    
            val portalItemId = "2e4b3df6ba4b44969a3bc9827de746b3"
            val portal = Portal("https://www.arcgis.com")
            val portalItem = PortalItem(portal, portalItemId)
    
            val layerId = 0
            val layer = FeatureLayer(portalItem, layerId.toLong())
    
            mapView.map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC).apply {
                operationalLayers.add(layer)
            }
    
            mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0))
    
        }
    
    Expand
  5. Add a done loading listener to the portal item. In the lambda, check if the portal item is not loaded and, in that case, display a Toast with the loading error.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 107 107
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
        // set up your map here. You will call this method from onCreate()
        private fun setupMap() {
    
            val portalItemId = "2e4b3df6ba4b44969a3bc9827de746b3"
            val portal = Portal("https://www.arcgis.com")
            val portalItem = PortalItem(portal, portalItemId)
    
            val layerId = 0
            val layer = FeatureLayer(portalItem, layerId.toLong())
    
            mapView.map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC).apply {
                operationalLayers.add(layer)
            }
    
            mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0))
    
            portalItem.addDoneLoadingListener {
                if (portalItem.loadStatus != LoadStatus.LOADED) {
                    val error = "Failed to load portal item ${portalItem.loadError.message}"
                    Log.e(TAG, error)
                    Toast.makeText(this, error, Toast.LENGTH_LONG).show()
                    return@addDoneLoadingListener
                }
            }
    
        }
    
    Expand

Click Run > Run > app to run the app.

The Android Emulator should display and run your app in the Android Virtual Devcie (AVD) selected in the Android Studio toolbar:

Currently selected AVD in Android Studio

If your app builds but no AVD displays, you need to add one. Click Tools > AVD Manager > Create Virtual Device...

You should see a map of trail heads in the Santa Monica mountains. Click, drag, and scroll the mouse wheel on the map view to explore the map.

What's next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials:

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