Android Open Source - arcgis-runtime-samples-android Main Activity






From Project

Back to project page arcgis-runtime-samples-android.

License

The source code is released under:

Apache License

If you think the Android project arcgis-runtime-samples-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* Copyright 2014 ESRI
 *//  w  w  w  . j  a va  2s  .c o m
 * All rights reserved under the copyright laws of the United States
 * and applicable international laws, treaties, and conventions.
 *
 * You may freely redistribute and use this sample code, with or
 * without modification, provided you include the original copyright
 * notice and use restrictions.
 *
 * See the Sample code usage restrictions document for further information.
 *
 */

package com.arcgis.android.samples.maps.switchmaps;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.esri.android.map.MapView;

/* This sample shows a way to switch between two different maps in a single app by leveraging the Android Fragments.
 * In this app, a single MapActivity contains a MapFragment that in turn contains a MapView. When the button on the
 * action bar is pressed, the existing MapFragment is replaced by a new instance of the MapFragment that has
 * different map contents. The current extent of the MapView is preserved when switching between fragments, by
 * making use of the retainState and restoreState methods on the MapView.
 */
public class MainActivity extends Activity {

    // service url string
    String topoUrl;
    String streetsUrl;

    // Action bar, and items for switching between maps. Visibility of these items
    // is changed, so that only one option is available at a time.
    ActionBar mActionBar;
    MenuItem mSwitchTo1MenuItem;
    MenuItem mSwitchTo2MenuItem;

    // Current map fragment state.
    boolean map1Active = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get service url from string resource
        topoUrl = getResources().getString(R.string.topo);
        streetsUrl = getResources().getString(R.string.streets);

        if (savedInstanceState == null) {
            // If no saved state exists, add a map fragment with the initial map
            // service layer.
            MapFragment mapFrag = new MapFragment();
            // arguments to send to MapFragment
            Bundle args = new Bundle();
            // service url
            args.putString("MAPURL", topoUrl);
            // opening extent null as it is set by view xml
            args.putString("MAPSTATE", null);

            mapFrag.setArguments(args);

            getFragmentManager().beginTransaction().add(R.id.fragmentContainer, mapFrag).commit();
        } else {
            // If there is saved state, then the fragment will be re-created by the android framework.
            // Extract the saved state of the activity from the bundle parameter.
            map1Active = savedInstanceState.getBoolean("map1Active", true);
        }

        // Set up the action bar.
        mActionBar = getActionBar();
        mActionBar.setTitle(R.string.app_name);

    }

    // Store temporary state so that this can be reinstated, for example if the device is rotated.
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save the activity state - which map fragment is currently active.
        outState.putBoolean("map1Active", map1Active);
    }

    // Update the action bar based on the saved state.
    private void updateActionBar() {
        // Update the action bar subtitle to indicate the current map.
        if (mActionBar != null) {
            mActionBar.setSubtitle(map1Active ? R.string.map1 : R.string.map2);
        }
        // Update the visible menu items to allow correctly switching maps.
        if (mSwitchTo1MenuItem != null) {
            mSwitchTo1MenuItem.setVisible(!map1Active);
        }
        if (mSwitchTo2MenuItem != null) {
            mSwitchTo2MenuItem.setVisible(map1Active);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        // Get references to the buttons on the menu for use in onOptionsItemSelected.
        mSwitchTo1MenuItem = menu.findItem(R.id.switchToMap1Button);
        mSwitchTo2MenuItem = menu.findItem(R.id.switchToMap2Button);

        // Initialize the state of the subtitles and buttons from the saved state.
        updateActionBar();

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle user pressing the action bar menu items.
        boolean retVal = false;

        // Get the current map directly from this activity.
        MapView currentMap = (MapView) this.findViewById(R.id.map);

        // Based on the menu item selected, switch the map fragment.
        String mapUrl = null;
        switch (item.getItemId()) {
            case R.id.switchToMap2Button:
                // Use the alternative map service as a layer.
                mapUrl = streetsUrl;
                map1Active = false;
                break;

            case R.id.switchToMap1Button:
                // Use the first map service as a layer.
                mapUrl = topoUrl;
                map1Active = true;
                break;

            default:
                retVal = super.onOptionsItemSelected(item);
                break;
        }

        if (mapUrl != null) {
            // Create a new fragment with a specific map service layer.
            MapFragment mapFrag = new MapFragment();
            // arguments to be sent to MapFragment
            Bundle args = new Bundle();
            // service url
            args.putString("MAPURL", mapUrl);
            // current extent
            args.putString("MAPSTATE", currentMap.retainState());
            mapFrag.setArguments(args);

            // Create a transaction and replace existing fragment with this new
            // fragment.
            FragmentTransaction ft2 = getFragmentManager().beginTransaction();
            ft2.replace(R.id.fragmentContainer, mapFrag);
            ft2.commit();

            // Update ActionBar subtitle, and action bar button visibility.
            updateActionBar();
            retVal = true;
        }

        return retVal;
    }
}




Java Source Code List

com.arcgis.android.samples.ExportTileCacheTask.ExportTileCacheTaskSampleActivity.java
com.arcgis.android.samples.cloudportal.featureservicetablequery.MainActivity.java
com.arcgis.android.samples.cloudportal.querycloudfeatureservice.MainActivity.java
com.arcgis.android.samples.dynamiclayer.DynamicLayerRendererActivity.java
com.arcgis.android.samples.geometrysample.BufferFragment.java
com.arcgis.android.samples.geometrysample.GeometrySampleActivity.java
com.arcgis.android.samples.geometrysample.GeometryUtil.java
com.arcgis.android.samples.geometrysample.SampleListFragment.java
com.arcgis.android.samples.geometrysample.SpatialRelationshipsFragment.java
com.arcgis.android.samples.geometrysample.UnionDifferenceFragment.java
com.arcgis.android.samples.localdata.localrasterdata.EditTextUtils.java
com.arcgis.android.samples.localdata.localrasterdata.FileBrowserFragment.java
com.arcgis.android.samples.localdata.localrasterdata.HillshadeRendererParametersFragment.java
com.arcgis.android.samples.localdata.localrasterdata.MainActivity.java
com.arcgis.android.samples.localdata.localrasterdata.OnDialogDismissListener.java
com.arcgis.android.samples.localdata.localrasterdata.RendererType.java
com.arcgis.android.samples.localdata.localrasterdata.StretchParametersFragment.java
com.arcgis.android.samples.maps.basemaps.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.BasemapListFragment.java
com.arcgis.android.samples.maps.fragmentmanagement.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.MapFragment.java
com.arcgis.android.samples.maps.helloworld.MainActivity.java
com.arcgis.android.samples.maps.maplegend.LegendDialogFragment.java
com.arcgis.android.samples.maps.maplegend.MainActivity.java
com.arcgis.android.samples.maps.maprotation.Compass.java
com.arcgis.android.samples.maps.maprotation.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MapFragment.java
com.arcgis.android.samples.milsym2525c.MainActivity.java
com.arcgis.android.samples.milsym2525c.Mil2525cMessageParser.java
com.arcgis.android.samples.oauth2sample.MapFragment.java
com.arcgis.android.samples.oauth2sample.OAuth2Sample.java
com.arcgis.android.samples.oauth2sample.UserContentActivity.java
com.arcgis.android.samples.oauth2sample.UserContentArrayAdapter.java
com.arcgis.android.samples.oauth2sample.UserContentFragment.java
com.arcgis.android.samples.oauth2sample.UserWebmaps.java
com.arcgis.android.samples.search.placesearch.MainActivity.java
com.esri.android.sample.closestfacilities.MainActivity.java
com.esri.android.samples.mbtiles.LocalMBTiles.java
com.esri.android.samples.mbtiles.MBTilesLayer.java
com.esri.android.samples.mgrsgrid.LocateMGRSActivity.java
com.esri.arcgis.android.sample.runtimegeodb.CreateRuntimeGeodatabaseActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapFragment.java
com.esri.arcgis.android.sample.simplemap.SingleFragmentActivity.java
com.esri.arcgis.android.samples.GeoJSONEarthquakeMap.GeoJSONEarthquakeMapActivity.java
com.esri.arcgis.android.samples.PopupUICustomization.LayerQueryTask.java
com.esri.arcgis.android.samples.PopupUICustomization.MyAttachmentsView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyEditAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyReadOnlyAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyTitleView.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupFragment.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupUICustomizationActivity.java
com.esri.arcgis.android.samples.addcsv2graphic.AddCSVActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeEditorActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeItem.java
com.esri.arcgis.android.samples.attributeeditor.AttributeListAdapter.java
com.esri.arcgis.android.samples.attributeeditor.FeatureLayerUtils.java
com.esri.arcgis.android.samples.basiclicense.MainActivity.java
com.esri.arcgis.android.samples.basiclicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.classbreaksrenderer.ClassBreaksRendererActivity.java
com.esri.arcgis.android.samples.featuredusergroup.FeaturedGroupsActivity.java
com.esri.arcgis.android.samples.featuredusergroup.GroupsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.ItemsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.MapActivity.java
com.esri.arcgis.android.samples.geometryeditor.ConfirmDiscardDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.EditFailedDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeData.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.GeometryEditorActivity.java
com.esri.arcgis.android.samples.identifytask.Identify.java
com.esri.arcgis.android.samples.measure.MainActivity.java
com.esri.arcgis.android.samples.nearby.Nearby.java
com.esri.arcgis.android.samples.offlineeditor.GDBUtil.java
com.esri.arcgis.android.samples.offlineeditor.OfflineActions.java
com.esri.arcgis.android.samples.offlineeditor.OfflineEditorActivity.java
com.esri.arcgis.android.samples.offlineeditor.TemplatePicker.java
com.esri.arcgis.android.samples.offlineroutingandgeocoding.RoutingAndGeocoding.java
com.esri.arcgis.android.samples.popupinwebmapforediting.PopupInWebmapForEditing.java
com.esri.arcgis.android.samples.popupinwebmapforviewing.PopupInWebmapForViewing.java
com.esri.arcgis.android.samples.querytask.MainActivity.java
com.esri.arcgis.android.samples.routing.MyAdapter.java
com.esri.arcgis.android.samples.routing.RoutingDialogFragment.java
com.esri.arcgis.android.samples.routing.RoutingListFragment.java
com.esri.arcgis.android.samples.routing.RoutingSample.java
com.esri.arcgis.android.samples.servicearea.EditFragment.java
com.esri.arcgis.android.samples.servicearea.ServiceAreaSample.java
com.esri.arcgis.android.samples.standardlicense.MainActivity.java
com.esri.arcgis.android.samples.standardlicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.standardlicenseoffline.MainActivity.java
com.esri.arcgis.android.samples.standardlicenseoffline.MessageDialogFragment.java
com.esri.arcgis.android.samples.uniquevaluerenderer.UniqueValueRendererSampleActivity.java
com.esri.arcgis.android.samples.viewshed.Viewshed.java
com.esri.arcgis.android.samples.wmslayer.MainActivity.java
com.esri.arcgis.samples.offlineanalysis.MainActivity.java