Skip To Content ArcGIS for Developers Sign In Dashboard


You will learn: how to build an app that loads and displays a layer from ArcGIS Online.

The ArcGIS Runtime SDK for Java allows you to access items (such as layers) stored in ArcGIS Online by their item identifier. Feature layers can store geographic features (points, lines, polygons), edit or update attributes, and synchronize changes. As demonstrated in the tutorial Display a web map, you can load and display an existing web map that was preconfigured with a basemap and layers. Alternatively, you may wish to load one or more layers from ArcGIS Online and create a map with code. You may do this if you have operational or base layers from different sources in your app. You can publish your own feature layer to ArcGIS Online using the Create a new dataset data tutorial.

In this tutorial you will learn how to access a layer stored in ArcGIS Online. You will open the layer using its identifier and display its contents on a map.

Before you begin

Reuse the starter project

If you have not done the starter project tutorial, be sure to review it first so you are familiar with running the app.

In a new or empty folder, make a copy of the Create a starter app solution.


Update the project

  1. Open the settings.gradle file in an editor of your choice and change the value to add-a-layer-from-an-item.


  1. Open the file in src/main/java/com/arcgis/developers/labs. Then add the following with the existing imports.

    import javafx.scene.control.Alert;
    import javafx.scene.control.Alert.AlertType;
    import com.esri.arcgisruntime.layers.FeatureLayer;
    import com.esri.arcgisruntime.loadable.LoadStatus;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.portal.Portal;
    import com.esri.arcgisruntime.portal.PortalItem;

Create and load portal item from portal

  1. Add a private method setupPortalItem that creates a Portal that establishes a connection to ArcGISOnline.

     private void setupPortalItem() {
       String portalItemId = "883cedb8c9fe4524b64d47666ed234a7";
       Portal portal = new Portal("");
  2. Create a PortalItem using the portal and a unique ID number. This is the unique ID for the Los Angeles Trailheads portal item.

     Portal portal = new Portal("");
     /* ** ADD ** */
     PortalItem portalItem = new PortalItem(portal, portalItemId);
  3. Wait for portal item to load and check load status.

     PortalItem portalItem = new PortalItem(portal, "883cedb8c9fe4524b64d47666ed234a7");
     /* ** ADD ** */
     portalItem.addDoneLoadingListener(() -> {
       if (portalItem.getLoadStatus() == LoadStatus.LOADED) {
       } else {
         new Alert(AlertType.ERROR, "Portal Item: " + portalItem.getLoadError().getMessage()).show();

Create layer from portal item and add to map

  1. Add a private method setupFeatureLayerFromPortalItem that creates a feature layer from a portal item and layer ID.

     private void setupFeatureLayerFromPortalItem(PortalItem portalItem) {
       int layerId = 0;
       FeatureLayer layer = new FeatureLayer(portalItem, layerId);
  2. Wait for layer to load then add layer to the map and set viewpoint to display all features within layer.

     FeatureLayer layer = new FeatureLayer(portalItem, 0);
     /* ** ADD ** */
     layer.addDoneLoadingListener(() -> {
       if (layer.getLoadStatus() == LoadStatus.LOADED) {
         mapView.setViewpoint(new Viewpoint(layer.getFullExtent()));
       } else {
         new Alert(AlertType.ERROR, "Feature Layer: " + layer.getLoadError().getMessage()).show();
  3. Uncomment the call to setupFeatureLayerFromPortalItem in setupPortalItem. In the start method, add a call to setupPortalItem after setupMap as follows:

     /* ** ADD ** */

Congratulations, you're done!

Your map and layer should load and display with the view point centered on the Los Angeles area and points displaying the trailheads.


Use your portal item

Instead of using the portal item we used above, use the one you made in the data tutorial Create a new dataset.

In setupPortalItem method, replace the value of portalItemId with the ID of your portal item.

Find the portal item URL

There are multiple ways to load a feature layer from ArcGIS Online. One way is to load the feature layer directly from a URL. This challenge is a follow-on to the challenge in Create a new dataset design tutorial.

Navigate to the page in ArcGIS Online for your portal item. In the item page, find the Service URL link and identify the service endpoint. This is the URL the runtime uses to access the data. It should look something like this:

Store the URL:

 // Hold the URL to the service
 String url = "";

Create the ServiceFeatureTable from URL with FeatureLayer

// Create the layer
ServiceFeatureTable table = new ServiceFeatureTable(url);
FeatureLayer layer = new FeatureLayer(table);

Add the layer to the map


Discover data

If you have not already done so, visit the Discover data tutorial and search for interesting open data you could add in another layer in your app.