My first application
The following document will guide you through the process of creating a new project and deploying it to either the Android SDK emulator or a device. The instructions and discussion below ultimately culminate into an application that is available as a sample in this application programming interface (API) help documentation, see Hello, world. This document's purpose is to help a developer understand the basics of working with the Esri provided plug-ins for the Eclipse integrated development environment (IDE) to build a basic application and understand fundamental principles that will help guide a developer in future application building. The main topics of discussion will be adding a map to your application with an initial extent and how to best address orientation changes.
- Prerequisites
- Create new ArcGIS project for Android
- Adding a map and setting an initial extent
- Executing the applicaiton
- Orientation change handling
Prerequisites
The workflows presented in this document assume that you have a development environment setup based on the system requirements, have the Esri plug-ins installed, and Eclipse opened with your workspace set. This document also assumes a basic understanding of the Android platform. Throughout this document, Android specific terms are used with the understanding that developers are familiar with the terminology of the Android platform. When possible, references are provided to Google's documentation for further reading for those who are less familiar with Android development. If some terminology is unfamiliar to you while you read this document, then it is strongly encouraged that you follow the supplied links and familiarize yourself with the language used in mobile development. This document will also point developers to useful Android documentation when setting up the emulator using an Android virtual device (AVD) configuration or device for deployment and testing, a very important part of your application development.
Create new ArcGIS project for Android
In this section you will start building your first application by creating a new ArcGIS project for Android using the plug-ins for Eclipse provided by Esri. Take the following steps to create your new project.
- From Eclipse's file menu, select new and click project.
- In the new project dialog, expand the ArcGIS for Android folder.

- Select the ArcGIS project for Android option and click next.
- In the new Android project dialog, fill out the form following the image.

The previous image shows you the build target set to a specific software development kit (SDK) of Android, but this specific version is not necessarily required by the ArcGIS API for Android. Consult the system requirements document for supported versions and select one of the supported versions as your build target. At any given release, it should not matter which SDK version you select, as long as you select one that is supported by the given release. If you decided to import the sample project instead of manually following this document's step-by-step instructions, then this information will have already been supplied correctly when the sample project was created and packaged with the product. The image is not intended to illustrate what is or is not supported for a given release, but rather to give context to creating a new ArcGIS project for Android. The workflow should be similar for any of the supported Android SDKs. In addition, the minimum SDK version follows a similar argument. The value placed here will depend on what Android SDK version your organization is targeting.
- When finished, click finish to create the new project.
When the project is first created in Eclipse, you might notice that the project has an error initially. This error is due to the fact that Eclipse has multiple threads to create a project and compile it. It is possible for the compiler to complain and give an error symbol for the project because the threads fall out of sync with one another. Most of the time, however, this issue resolves itself once all the threads complete their tasks and sync up with one another, but it is possible for the error to remain. If this is the case, this issue can be resolved when the following is done:
- From Eclipse's Project menu, click Clean....
- In the clean dialog you are presented with an option to clean all your projects or to clean projects selected from a list. The easiest option is to select only the particular project that you have created; however, you can use the other option to clean multiple projects at once. The image presented next is only an example of the clean dialog in Eclipse. This dialog will reflect the current status of opened projects in your Eclipse workspace and will look slightly different.

- Once you have decided which approach to take, click OK to clean your Eclipse project(s).
If you have the progress view enabled in Eclipse, then you will see the status of the project being recompiled. This step should resolve any of the build issues that might linger when the project is initially created.
You have now created a new ArcGIS project for Android successfully. Next, you will add a map service to the application and setup an initial extent for that map.
Adding a map and setting an initial extent
Now that you have created a new ArcGIS project for Android the next step is to get a map added to the application. The map we are interested in adding to this particular application is the world-wide street map available from ArcGIS.com. However, we are not interested in displaying the street map for the entire world when the map is initialized. It is often more desirable for the application to initially display a map for a specific region instead of its entire extent - in this instance, a specific region of Southern California as opposed to the entire world. The following discussion will guide you through the process of adding a map and specifying an initial extent for the application.
To begin, take the following steps to add a map to your project.
- In your new project, expand the res and then the layout folder.
- Double-click on main.xml to open it in Eclipse's Android extensible markup language (XML) editor. You may need to explicitly switch the editor view to 'XML' by hitting the XML tab at the bottom of the editor window.
By default there are two tags in the main.xml. The first is Android's linear layout, which is a layout that arranges its children views in a single column or a single row. To understand this layout template further, see the LinearLayout documentation provided by Google. LinearLayout is just another Android View, but since it's purpose is to organize children views that are added to it, it can be referred to as a parent view. For a detailed explanation of the Android View hierarchy, see the User Interface documentation. The second tag is the TextView tag, considered the child View in this example. This tag is used to add text to the application, in this case "Hello World, HelloWorld!". If you follow Android's Hello, World tutorial, this project in its current form represents Android's first basic application.
- Delete the LinearLayout and TextView tags from main.xml.

- With the cursor position at line 2 in the XML editor (see previous image), right-click in the XML editor, choose ArcGIS Tools and click Add Map View.

- Inside the Add Map Services dialog, click Connect to connect to ArcGIS.com.
By default the add map services dialog is set to connect to ArcGIS.com. This address can be replaced with the uniform resource locator (URL) to your own ArcGIS Server instance. The purpose of this dialog is to help expedite the process of finding and adding map services to your application. Since maps and layers ultimately translate to Android View objects using the ArcGIS API, these maps are configured directly in a project's layout resource location (in this example, main.xml is used, but it does not have to be this XML file and can be any developer defined layout resource in an Android project).
- Expand the URL on the left-hand-side to see the list of map services. Double-click World_Street_Map (MapServer). This action will place the map service on the right-hand-side of the dialog.
The items represented on the left-hand-side of the map service selection dialog represent the map services that are available from the supplied server. The items that appear on the right-hand-side of this dialog represent the map services that you wish to add to your layout resource XML file. This dialog is designed to be intuitive to the user with additional helpful hints and instructions at the bottom of the dialog. You can use this dialog to add and remove services as necessary for your application. In this example, we only wish to add a single service, but more than one can be added to a project. In addition, when the map service is added to the right-hand-side of this dialog, there is additional information supplied to let you know the type of service you are working with and coordinate system information. In this example, the world street map service is a tiled service, meaning it has been cached on the server side, and that it was cached using the well known coordinate system id 102100. Using the coordinate system IDs reference document, it can be determined that this dataset was projected using the world geodetic system (WGS) 1994 web mercator auxiliary sphere coordinate system. Such information is vital when combining more than one map service to your application. For example, if you have more than one cached map service, they must have a matching coordinate system.
- Click OK on the map service selection dialog to add the world street map to your application.
- Click OK to add the appropriate permissions to your Android application.
Every Android application must have an AndroidManifest.xml (located at the root folder of every Android project in Eclipse) file as part of the Android package (APK) file - used to distribute and install applications onto an Android device. One important aspect of this file is the declaration of permissions that your application must have in order to execute on a device. The permissions dialog that appeared in Eclipse once you finished using the map service selection dialog to configure your map services asked to confirm the addition of three specific permissions to the Android manifest file. The following are descriptions of each of these permissions that are added to the manifest file.
- android.permission.INTERNET: This will allow your application to open network sockets. This is very important because the ArcGIS API for Android only works in connected mode, meaning it must be allowed to open network connections to communicate with ArcGIS Server.
- android.permission.ACCESS_FINE_LOCATION: This permission allows your application to access the global positioning system (GPS) information from a device. Programmatically it is possible for you to work with the LocationService class in the ArcGIS API, which supports the display of the current location on the MapView. However, the reason for the permission is due to the tight integration of the LocationService class in the ArcGIS API and the LoationManager class from the Android SDK. The LocationManager is what provides the access to a devices location services and allows the ArcGIS API to obtain periodic updates of a device's geographic location to be displayed on a map. If you do not have a need for the geographic location in your application, then you can remove this permission from your Android manifest file. By default the Esri provided plugins assume that this functionality is needed and includes it by default. To remove the permission, you can manually edit the AndroidManifest.xml file or use the Android development tool (ADT) plug-in for Eclipse and select the permissions tab of the manifest file.
- android.permission.WRITE_EXTERNAL_STORAGE: This permission allows your application to write data to an external storage medium, such as a secure digital (SD) card. In order to optimize the performance of your applications that use the ArcGIS API this permission is vital and required.
These permissions, as described, are key to any project you implement for your Android applications and relate strictly to the ArcGIS API. You can see these additions to your Android project by opening the Android manifest file found at the root of your Eclipse project. Once opened, you can observe the raw XML by selecting the AndroidManifest.xml tab.

To learn more about the Android manifest file and its composition, see The AndroidManifest.xml File document. It might be necessary for you to include additional permissions beyond the three described above in your manifest file, the following Java documentation gives you an extensive list of the constants, Manifest.permission. If you do not utilize the map service selection dialog in future application development, then you must manually remember to add these permissions to your project before you deploy it.
- If you opened the Android manifest file to follow the previous discussion, return back to the main.xml file in Eclipse.
Notice the new XML code added to the main.xml file. The map service selection dialog defined the proper XML to add the MapView and ArcGISTiledMapServiceLayer to your layout file. In particular, two new XML tags have been added to your application: <com.esri.android.map.MapView> and <com.esri.android.map.ags.ArcGISTiledMapServiceLayer>. The first tag is used to represent your map and is considered the parent view, similar to the default LinearLayout View described previously. The second tag is used to represent the ArcGIS Server cached map service. This tag represents the child View, just like the default TextView. Before you can learn how to execute this application, you need to learn how to set an initial extent of the map so when the application is first initialized it will display a specific region of California that encompasses the greater Los Angeles area as well as San Diego.
- Add the following attribute and value to the <com.esri.android.map.MapView> tag: initExtent="-1.3296373526814876E7 3930962.41823043 -1.2807176545789773E7 4201243.7502468005"

The initial extent that you set in your layout resource file with be based on the default spatial reference of the map. The map's spatial reference is determined by the first layer that is retrieved and added to the map. This implies that your coordinates must be supplied in that spatial reference's units. The coordinates that are supplied must be in the following order: xMin, yMin, xMax, yMax, and the values are delimited by a space. These coordinates will define the minimum bounding rectangle that defines the initial extent of your map. This extent, once set, cannot be changed dynamically through code, even though it is possible to change the extent of the map dynamically using the MapView's setExtent() method. The initial extent is a special circumstance that can only be defined as part of your layout resource file. The previous illustration of the initial extent attribute and value has been modified to make the source code more readable. When you add this attribute and value to your main.xml, you must ensure that all the required initial extent values are delimited with a single space; otherwise, the initial extent will not work when the map is initialized and will resort to the map service's intial extent.
You have now completed the steps necessary to add a map and set an initial extent to a new ArcGIS project for Android. For a deeper understanding into the declarative approach to user interface (UI) design, see Declaring Layout. Next, you will learn how to take this basic application and deploy it to the emulator, then you'll learn how to deploy this same application to a device.
Executing the application
The Android platform offers a number of mechanisms for deploying and testing your applications. The first that is examined in this document is the device emulator that is included with the Android SDK. This is essentially a simulation of a device that can run on your computer and allows you to build, test, and debug your ArcGIS API applications on the Android platform. To understand more about what the emulator is and what can be done with it, see the Android Emulator documentation for extensive details. In order for you to test your applications more effectively, the emulator supports multiple Android Virtual Devices, or AVD configurations. For instance, AVDs allow you to specify a specific Android platform to run your applications on when testing with the emulator. A strong advantage of the AVD configuration is that you can create as many as necessary to simulate all of your planned deployment devices. However, you should not rely only on the emulator as your only means of testing and you should always test on a device. There are significant differences between the emulator and the physical device, such as multi-touch support, that requires testing on a device and cannot be simulated with the emulator. Thus, the second approach examined in this document is a device. Most Android devices typically connect with your computer through a universal serial bus (USB) port and the section below will point you to Android's documentation for setting up your device connection with your development machine. Both of these approaches will be discussed based on the application that you've been building as part of this document.
Using the Android SDK emulator
This section will look at defining an AVD configuration and then using it to execute your first Android application. The AVD that you will create for this application will be very minimal and simple. This document also works with a specific version of the Android SDK, but the process can be changed to work with supported Android platforms, you should always consult the system requirements and the steps below should change minimally if you choose a different SDK level as your target.
Take the following steps to create an AVD configuration and deploy your application in Eclipse:
- Select the window menu option and click Android SDK and AVD manager.
- Verify that the virtual devices item is selected from the left panel of the dialog. If not, then click the option to get a list of available AVDs.
If you have already defined and created an AVD, then you will see a list of these configurations on this panel. You can use any one of your existing AVDs to run this application. However, if this is your first time, then you will have no available AVDs and need to complete the remaining instructions to create your first AVD configuration.
- Click new to create a new AVD.
- Fill in the create new android virtual device dialog based on the image.

The previous image shows the target set to a specific SDK of Android, but this specific version is not necessarily required by the ArcGIS API for Android. Consult the system requirements document for supported versions and select one of the supported versions as your target. At any given release, it should not matter which SDK version you select, as long as you select one that is supported. If you do not see a supported version, then you might not have properly added the SDK components to your development machine. For a description on the process of adding Android SDK components to your development machine, see Adding SDK Components. The remaining field values in the dialog use fictitious values in the sense that they are not intended to simulate a specific device, but rather demonstrate one possible AVD configuration. To refine your AVD configurations, consult the Android Virtual Devices documentation. The workflow for creating an AVD should be similar for any of the supported Android SDKs.
- Click create AVD to create your configuration.
You should now see the new AVD configuration in Eclipse's Android SDK and AVD manager dialog.

- Close the Android SDK and AVD manager in Eclipse.
- Right-click your HelloWorld project in Eclipse, choose run as and click Android application to execute the application.
This process should startup the Android emulator on your computer using the configuration you just created. The emulator is quite intensive and may take some time to load. After the emulator has been initialized, you will see your HelloWorld application, your first ArcGIS application for the Android platform, running. The following is an image of the map you should see in your emulator when the activity first initializes.

You have successfully deployed your first ArcGIS application for the Android platform using the emulator. Take a moment to pan around the map by using your mouse to simulate a finger drag gesture, and so on. Alternatively, you can also deploy the same application to a device. The next section takes you through this process.
Using a device
This section will look at pointing you to resources that will help you setup your device so it can be used as another avenue for prototyping, testing and debugging your applications. When testing your applications it is imperative that you test on the device that you will deploy your production application too. Android has built extensive tooling to allow your device to work with Eclipse during the testing and debugging phases of the development life cycle. See Developing on a Device for some initial literature on developing applications using a device. To begin running your applications on a device, you need to perform the following items for your device.
- Check that your development machine can discover your device when it is connected through a USB port.
- Enable the USB debugging mode on your device.
- Declare your application as debuggable.
The following steps will help you achieve the above tasks:
- Plug your device, using its connecting cable, into a free USB slot on your development computer.
If this is the first time you've plugged your device into your machine, it will attempt to recognize and install a driver for your device. However, you should check with your particular phone's vendor for device and operating system specific drivers, download them, and install those on your development machine. This process will vary depending on the vendor and device model you are working with, but each vendor's web site should contain information and downloads for your particular device. You should also consult the USB Driver for Windows documentation for information about the latest version of the USB driver and installation instructions when working with the Android SDK.
- Enable the USB debugging mode on your device. To do this, go to your device's home screen, press menu, select settings. From the settings menu select applications, then development. Enable USB debugging by selecting this option from the development menu. This workflow might vary slightly depending on the device you are working with (consult your vendor's documentation if you are unable to enable this mode.
- If a warning message is displayed on your phone about what USB debugging is intended for, review it, and select OK to close it.
- To verify that your device is able to communicate with the Android SDK, open a command window and type: adb devices. If your device is connected, then you'll see the name listed as a device in the command prompt. If you do not see your device listed, then you have likely not connected your device to your development machine correctly. See step 1's information section for referenced documentation to help you setup your device. You might also need to consult the device vendor's web site for any special requirements. Here is one example of a connected device to a development machine.

The Android debug bridge (ADB) is a versatile tool available from the Android SDK that allows you to manage the state of an emulator or powered device. If the command above does not work for you, then look at Step 2. Downloading the SDK Starter Package for details on setting up your development machine's PATH environment variable to ensure you have configured your development machine correctly. To learn about the ADB in depth, see the Android Debug Bridge documentation.
You'll also want to verify that Eclipse can also communicate with your device using the ADT plug-in. To do this, follow the next set of steps.
- If Eclipse was opened when you enabled the USB debugging mode in step 3, then close Eclipse and re-open it.
- Enable the ADT LogCat view in Eclipse - select the window menu, then click show view and select the other option.

- In the show view dialog, expand the Android folder, select LogCat, and click OK.
Use your phone to start up various applications and notice the logging information that is displayed in the LogCat view. For example, the following image shows you an instance of what the LogCat might look like when reporting log details from a device.

Android's ADT plug-in for Eclipse provides a mechanism for collecting and viewing system debug output. You can learn more about this integration piece by reading Enabling logcat Logging. Now that you have setup your development machine to communicate with your device, the final step is to declare your application as debuggable, and then execute it.
- In Eclipse, open the AndroidManifest.xml file.
- Add android:debuggable="true" to the <application> element in this file as shown on line 2 of the image.

You have now completed the necessary setup instructions for device development. Next you will deploy your first application to your phone.
- Right-click your HelloWorld project in Eclipse, choose run as and click Android application to execute the application.
This process should package your Android application and install the APK file on your device. It will also attempt to start your default activity using its intent. After the application is launched, you should see the same map as in the following image.

You have successfully deployed your first ArcGIS application for the Android platform to a device. Take a moment to pan around the map using your finger to create various gestures. By default, you will be able to:
- Pan the map using a single finger drag gesture.
- Zoom-in by double-tapping or pinching-out gestures.
- Zoom-out by a double-finger, double-tap or pinch-in gestures.
Orientation change handling
When your first mapping application is running in either the emulator or a device it is possible for the emulator or device to be rotated from a portrait to landscape orientation or the other way around. When this rotation occurs the Android system automatically handles the orientation change events and restarts the activity with the new configuration. More specifically, the activity that is currently executing when the rotation occurs is destroyed and recreated causing the activity to go through the following life cycle changes: onPause() -> onStop() -> onDestroy(), then immediately to the onCreate() of the same activity, and then the activity's onStart() -> onResume() methods. To understand more about the activity life cycle, see Activity life cycle for detailed documentation. When the mapping application is running and either the emulator or phone is rotated, you might have noticed that the map returns to its initial state - showing the map with its initial extent as discussed in the previous workflow of this document. If you did make this observation while working through the steps above, then take the following steps to observe this effect.
- Run the mapping application using either the emulator or device.
- On the map, pan and zoom to the Salton Sea so it is the center of your map, as illustrated in the following image (the Salton Sea can be located just below Coachella of the Palm Desert).

- Rotate your device or the emulator (Use Ctrl + F11 to rotate the emulator and simulate a device rotation).

Notice that the application now displays the original map using the initial extent you set. This is because your mapping application activity is destroyed and then quickly re-created, which means the onCreate() method is called and the application re-initialized to fit the new orientation. To address this issue, Android provides a special method on the Activity class called onRetainNonConfigurationInstance(). This method allows Android developers to pass an arbitrary object to a future self. We will override this method and use a convenience method on the MapView class to address this rotation scenario. Execute the following steps to your HelloWorld.java class file.
- In your project, expand the src and package folders in your HelloWorld project.
- Double-click HelloWorld.java to open it in Eclipse's editor.
- Declare an instance variable of type MapView called map. Assign null to this variable in the declaration statement.
- Import the appropriate package for the MapView class to eliminate the source code error.
- In the Eclipse editor, right-click and select source, then click on override/implement methods.
- In the override/implement methods dialog, navigate to the onRetainNonConfigurationInstance() method, and check the checkbox.

- Click OK to insert the new method into your Java class.
- Remove the comment and return statement, and write the following single line of code.
Override the Activity's onRetainNonConfigurationInstance() method and add the following line of code to the method definition.
return map.retainState();
The retainState() method of the MapView class will provide two valuable pieces of information that are necessary to restore the state of the map after the rotation occurs: the center of the map, and the current map resolution. This information is returned to you as a String object and the code above returns the String to satisfy the method signature.
- Within the onCreate() method already defined in your Java file, add the following code.
The following code will allow you to restore the state of your map after the rotation occurs. Add this code after the setContentView() method.
map = (MapView) findViewById(R.id.map); Object init = getLastNonConfigurationInstance(); if (init != null){ map.restoreState((String) init); }The code that is added to your onCreate() method will obtain the object you defined when you overrode the onRetainNonConfigurationInstance() method. If the Activity was destroyed/created automatically, then this object that is returned to you will not be null and thus can be used to restore the state of your map. This is done through another convenience method, restoreState(), where you pass in the last non-configuration instance object that is returned. In this scenario, we know that this object is a String, since that is how we defined it and thus we cast to a String before passing the object into the method. The restoreState() method of the MapView class does the job of restoring the center and resolution of the MapView.
- Re-deploy your application and re-test the same scenario presented above.
Notice that when you rotate the emulator or device that the map now displays the map as expected.
If you use the emulator to simulate the rotation, then when the emulator is positioned back to the portrait orientation it will revert back to the initial extent of the map. This behavior is a bug and illustrates another reason why you must test your applications on a device. If you used a device to simulate the rotation, then the application will work as expected.
This completes the discussion on handling rotation for your first mapping application. The previous explanation can be understood in greater detail by reviewing the Faster Screen Orientation Change documentation provided by Google. This documentation will stress the importance of only using this approach to retain data that is expensive to load and you might wonder why this approach is used in this document. The technique described in this document will become of extreme importance when working with the GraphicsLayer and individual graphics that can be expensive to load.
This concludes the development of your first ArcGIS API application for the Android platform. As stated at the beginning of this document, this project is available to you as a sample. You can learn about how to work with samples here, while the actually sample's documentation can be found here.