Selecting graphics

Selection in the layer

You may want to select graphics in your application after some action was taken, for example in response to a mouse click, or after running a query. A graphic can be selected in a graphics layer via its unique graphic ID. The methods supporting selection are members of the GraphicsLayer class. To select features in a feature layer, the same methods are used, as the feature layer class ArcGISFeatureLayer inherits from the GraphicsLayer class. The code snippet below shows how you would select graphics in response to a mouse click, where 'hitGraphicIDs' are the graphics hit by the latest click of the mouse:

for (int id : hitGraphicIDs) {
    if (graphicsLayer.isGraphicSelected(id)) {
        // if graphic is selected in the layer, unselect it
    } else {
        // otherwise select graphic in the layer;

To obtain the graphics hit by the mouse click, you could use the SDK toolkit's HitTestOverlay which responds to mouse clicks on the map and stores the latest clicked graphics. You could also use the API directly by calling the getGraphicIDs method on the graphics layer, which takes an x and y coordinate, as well as a pixel tolerance around this coordinate, and optionally a maximum number of results to return, and returns the graphics within this range.

Rendering the selected graphics

In order to render graphics which are selected in a layer, the API's default behavior is to render the graphics with a colored cyan outline for all types of graphics. When the graphic is then unselected, the colored outline will no longer be shown. An example of what selected graphics look like is below:

selected graphics

You can control the color of this outline for each graphics layer and feature layer and you can choose not to have a colored outline at all. There is one selection color per graphics layer or feature layer and if you change this color on-the-fly, for example when graphics are already selected in the layer, they will start showing with the new selection color once it is set. The code snippet below shows how you would set a yellow colored outline for selected graphics in your graphics layer:

// create a graphics layer
graphicsLayer = new GraphicsLayer();
// set the selection color - default is CYAN color
// ... add graphics to the layer

As mentioned, if you make no calls to the setSelectionColor method, your selected graphics will be rendered with a cyan (light blue) outline. If you do not want selected graphics to be rendered with a colored outline, you can set a selection symbol on the layer instead. When you set a selection symbol for a layer, the symbol will be shown instead of the colored outline when a graphic or feature is selected.


Setting a selection symbol on a layer is not recommended unless all your graphics or features are of the same geometry type and thus can be rendered with the same symbol.

The code snippet below shows how you would set a selection symbol on a points feature layer:

ArcGISFeatureLayer citiesLayer = new ArcGISFeatureLayer(
// set a renderer for the feature layer
citiesLayer.setRenderer(new SimpleRenderer(new SimpleMarkerSymbol(Color.RED, 12, Style.CIRCLE)));
// set a selection symbol for when a feature is selected in the layer
citiesLayer.setSelectionSymbol(new SimpleMarkerSymbol(Color.YELLOW, 12, Style.CIRCLE));
// add the layer to your map