Search code examples
androidarcgis

How to view ArcGIS layer legend in Android?


I have 3 layers (Drainage, Bridge and Slope) in my ArcGIS Feature Server. My Android App view each layer on ArcGISmap, and set layer visibility based on checkbox tick. I am using ArcGIS Runtime SDK for Android (version 100.1).

My question is, is it possible to view the layers' legend from feature server into my Layers dialog? Means to replace the 3 cute icons.

Android App (Layers dialog in the middle)

enter image description here

Feature Server (layers' legend on left)

enter image description here

Thank you in advance!


UPDATE

Thanks to falldownhill's suggestion to use Create Swatch! This is my working code

// Loop each layer
for (int x=0; x<featureLayer.length; x++)
{
    try
    {
        // Try get each layer legend
        final ListenableFuture<List<LegendInfo>> layerLegend = featureLayer[x].fetchLegendInfosAsync();

        final int finalX = x;
        layerLegend.addDoneListener(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    // Get each layer legend
                    List<LegendInfo> legendInfo = layerLegend.get();
                    Symbol legendSymbol = legendInfo.get(0).getSymbol();
                    ListenableFuture<Bitmap> symbolSwatch = legendSymbol.createSwatchAsync(MainActivity.this, Color.TRANSPARENT);

                    // Set each layer legend
                    Bitmap symbolBitmap = symbolSwatch.get();
                    ImageView swatchImg = (ImageView) drawerDialog.findViewById(layerImageViewId[finalX]);
                    swatchImg.setImageBitmap(symbolBitmap);
                }
                catch (InterruptedException e)
                {
                    //showMessage("Animation interrupted");
                }
                catch (ExecutionException e)
                {
                    // Deal with exception during animation...
                }
            }
        });
    }
    catch (Exception e)
    {
        // Ignore changing layer legend
    }
}

Android App (Updated layers dialog with desired legend in the middle)

enter image description here


Solution

  • I think Create Swatch will do what you're looking for.

    Just to show one way to use it:

    float density = InstrumentationRegistry.getContext().getResources().getDisplayMetrics().density;
    for (FeatureType type : featureTable.getFeatureTypes()) {
      for (FeatureTemplate template : type.getTemplates()) {
        Geometry geometry = //create whatever shape you want the swatch to be
        Feature feature = featureTable.createFeature(template, geometry);
        Symbol symbol = featureLayer.getRenderer().getSymbol(feature);
        ListenableFuture<Bitmap> symbolSwatch = symbol.createSwatchAsync(24, 24,
            density,color,feature.getGeometry());
    
        Bitmap swatch = symbolSwatch.get();
      }
    }