Control annotation sublayer visibility

View on GitHub
Sample viewer app

Use annotation sublayers to gain finer control of annotation layer subtypes.

Image of control annotation sublayer visibility

Use case

Annotation, which differs from labels by having a fixed place and size, is typically only relevant at particular scales. Annotation sublayers allow for finer control of annotation by allowing properties (like visibility in the map and legend) to be set and others to be read (like name) on subtypes of an annotation layer.

An annotation dataset which marks valves as "Opened" or "Closed", might be set to display the "Closed" valves over a broader range of scales than the "Opened" valves, if the "Closed" data is considered more relevant by the map's author. Regardless, the user can be given a manual option to set visibility of annotation sublayers on and off, if required.

How to use the sample

Start the sample and take note of the visibility of the annotation. Zoom in and out to see the annotation turn on and off based on scale ranges set on the data.

Use the checkboxes to manually set "Open" and "Closed" annotation sublayers visibility to on or off.

How it works

  1. Load a MobileMapPackage that contains an AnnotationSublayer.
  2. Get the sublayers from the map package's layers by calling sublayer.getSubLayerContents.get(i).
  3. You can toggle the visibility of each sublayer manually using sublayer.setVisible().
  4. To determine if a sublayer is visible at the current scale of the MapView, use sublayer.isVisibleAtScale(), by passing in the map's current scale.

Relevant API

  • AnnotationLayer
  • AnnotationSublayer
  • LayerContent

Offline Data

  1. Download the data from ArcGIS Online.
  2. Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
  3. Push the data into the scoped storage of the sample app: adb push GasDeviceAnno.mmpk /Android/data/com.esri.arcgisruntime.sample.controlannotationsublayervisibility/files/GasDeviceAnno.mmpk

About the data

The scale ranges were set by the map's author using ArcGIS Pro:

  • The "Open" annotation sublayer has its maximum scale set to 1:500 and its minimum scale set to 1:2000.
  • The "Closed" annotation sublayer has no minimum or maximum scales set, so will be drawn at all scales.


annotation, scale, text, utilities, visualization

Sample Code
 *  Copyright 2019 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
 * 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.arcgisruntime.sample.annotationsublayer;

import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.esri.arcgisruntime.layers.AnnotationLayer;
import com.esri.arcgisruntime.layers.AnnotationSublayer;
import com.esri.arcgisruntime.layers.Layer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.MobileMapPackage;
import com.esri.arcgisruntime.mapping.view.MapView;

public class MainActivity extends AppCompatActivity {

  private static final String TAG = MainActivity.class.getSimpleName();

  private MapView mMapView;
  // objects that implement Loadable must be class fields to prevent being garbage collected before loading
  private MobileMapPackage mMobileMapPackage;

  protected void onCreate(Bundle savedInstanceState) {

    // get a reference to the map view
    mMapView = findViewById(;

    // show current map scale in a text view at the bottom of the screen
    TextView currentMapScaleTextView = findViewById(;
    mMapView.addMapScaleChangedListener(mapScaleChangedEvent -> currentMapScaleTextView
        .setText(getString(R.string.map_scale, Math.round(mMapView.getMapScale()))));

    // get a reference to checkboxes
    CheckBox closedCheckBox = findViewById(;
    CheckBox openCheckBox = findViewById(;

    // load the mobile map package
    mMobileMapPackage = new MobileMapPackage(getExternalFilesDir(null) + getString(R.string.gas_device_anno_mmpk_path));
    mMobileMapPackage.addDoneLoadingListener(() -> {
      if (mMobileMapPackage.getLoadStatus() == LoadStatus.LOADED) {
        // set the mobile map package's map to the map view
        // find the annotation layer within the map
        for (Layer layer : mMapView.getMap().getOperationalLayers()) {
          if (layer instanceof AnnotationLayer) {
            // load the annotation layer. The layer must be loaded in order to access sub-layer contents
            layer.addDoneLoadingListener(() -> {
              // get annotation sublayer name from sublayer contents
              AnnotationSublayer closedLayer = (AnnotationSublayer) layer.getSubLayerContents().get(0);
              AnnotationSublayer openLayer = (AnnotationSublayer) layer.getSubLayerContents().get(1);

              // set the layer name from the

              // toggle annotation sublayer visibility on check
                  (checkBoxView, isChecked) -> closedLayer.setVisible(isChecked));
                  (checkBoxView, isChecked) -> openLayer.setVisible(isChecked));

              // when the map scale changes
              mMapView.addMapScaleChangedListener(mapScaleChangedEvent -> {
                // if the "open" layer is visible, set text color to black, otherwise set it to gray
                if (openLayer.isVisibleAtScale(mMapView.getMapScale())) {
                } else {

      } else {
        String error = "Mobile map package failed load: " + mMobileMapPackage.getLoadError().getMessage();
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
        Log.e(TAG, error);

   * Get name, and where relevant, append min and max scales of each annotation sublayer.
   * @param annotationSublayer
   * @return the layer name with min max scales, where relevant
  private String buildLayerName(AnnotationSublayer annotationSublayer) {
    StringBuilder layerNameBuilder = new StringBuilder(annotationSublayer.getName());
    if (!Double.isNaN(annotationSublayer.getMaxScale()) && !Double.isNaN(annotationSublayer.getMinScale())) {
      layerNameBuilder.append(" (1:").append((int) annotationSublayer.getMaxScale()).append(" - 1:")
          .append((int) annotationSublayer.getMinScale()).append(")");
    return layerNameBuilder.toString();

  protected void onPause() {

  protected void onResume() {

  protected void onDestroy() {