Android Open Source - arcgis-runtime-samples-android Popup U I Customization 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 va2  s  . co 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.esri.arcgis.android.samples.PopupUICustomization;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Toast;

import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.android.map.event.OnLongPressListener;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.map.popup.Popup;
import com.esri.android.map.popup.PopupContainer;
import com.esri.android.toolkit.map.MapViewHelper;
import com.esri.android.toolkit.map.PopupCreateListener;
import com.esri.arcgis.android.samples.PopupUICustomization.PopupFragment.OnEditListener;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.FeatureEditResult;
import com.esri.core.map.Graphic;

/*
 * This sample shows how to customize the UI of a pop-up. The layout and style of a pop-up can be 
 * changed through XML and extending some built-in classes of the pop-up API.
 * 
 * Single-tap will bring up pop-ups with customized UI. Long-press will display pop-ups of default UI for comparison. 
 * A helper class from Application Toolkit called MapViewHelper is used to create pop-ups with default UI. 
 */

public class PopupUICustomizationActivity extends FragmentActivity implements OnEditListener {
  
  private MapView mMapView;
  private MapViewHelper mMapViewHelper;
  private PopupFragment mPopupFragment;
  
  private final static String URL = "http://www.arcgis.com/home/item.html?id=c93081d0ca924317bebf2b362d3bc8de";
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Display a web map.
    mMapView = new MapView(this, URL, "", "");
    
    setContentView(mMapView );
    
    mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
     
      private static final long serialVersionUID = 1L;

      @Override
      public void onStatusChanged(Object source, STATUS status) {
        if ((status == STATUS.INITIALIZED) && (source == mMapView)) {
          // Create a MapViewHelper object once the map view has been initialized.
          mMapViewHelper = new MapViewHelper(mMapView);
        }
      }
    });
    
    // Query all the layers within the mapview and customized the UI of the pop-ups
    // in async tasks.
    mMapView.setOnSingleTapListener(new OnSingleTapListener() {
      
      private static final long serialVersionUID = 1L;
      
      @Override
      public void onSingleTap(float x, float y) {
        if (!mMapView.isLoaded())
          return;
        
        mPopupFragment = new PopupFragment(mMapView);
        LayerQueryTask queryTask = new LayerQueryTask(mMapView, mPopupFragment);
        queryTask.queryLayers(x, y, 20, true);
      }
    });
    
    // Query all the layers and display pop-up with default UI using MapViewHelper
    // using a helper class of Application Toolkit.
    mMapView.setOnLongPressListener(new OnLongPressListener() {
      
      private static final long serialVersionUID = 1L;

      @Override
      public boolean onLongPress(float x, float y) {
        
        mPopupFragment = null;
        // The helper class from Application Toolkit will loop through and query each layer in the mapview.
        // A pop-up will be created for each feature in the query result and will be added to a PopupContainer.
        // The user-define PopupCreateListerner will be called when a pop-up is created.
        // User can put their logic in the PopupCreateListerner to display the pop-ups.
        mMapViewHelper.createPopup(x, y, new SimplePopupCreateListener());
        return true;
      }
    });
  }
  
  @Override
  public void onDelete(ArcGISFeatureLayer featureLayer, Popup popup) {
    // Commit deletion to server
    Graphic graphic = (Graphic)popup.getFeature();
    if ((graphic != null) && (featureLayer != null)) {
      featureLayer.applyEdits(null, new Graphic[]{graphic}, null, 
          new EditCallbackListener(this, featureLayer, popup, true, "Deleting feature"));
    }
    
    // Dismiss pop-up
    this.getSupportFragmentManager().popBackStack();
  }
  
  @Override
  public void onEdit(ArcGISFeatureLayer featureLayer, Popup popup) {
    // Set pop-up into editing mode
    popup.setEditMode(true);
    // refresh menu items
    this.invalidateOptionsMenu();
  }
  
  @Override
  public void onSave(ArcGISFeatureLayer featureLayer, Popup popup) {
    // Commit edits to server
    Graphic graphic = (Graphic)popup.getFeature();
    if ((graphic != null) && (featureLayer != null)) {
      Map<String, Object> attributes = graphic.getAttributes();
      Map<String, Object> updatedAttrs = popup.getUpdatedAttributes();
      for (Entry<String, Object> entry : updatedAttrs.entrySet()) {
        attributes.put(entry.getKey(), entry.getValue());
      }
      Graphic newGraphic = new Graphic(graphic.getGeometry(), null, attributes);
      featureLayer.applyEdits(null, null, new Graphic[]{newGraphic}, 
          new EditCallbackListener(this, featureLayer, popup, true, "Saving feature"));
    }
    
    // Dismiss pop-up
    this.getSupportFragmentManager().popBackStack();
  }
  
  // TODO: add your logic to handler click event of image buttons shown in a pop-up
  public void addFavorite(View view) {
    
  }
  
  public void findDirection(View view) {
    
  }
  
  public void share(View view) {
    
  }
  
  
  @Override
  protected void onPause() {
    super.onPause();
    mMapView.pause();
  }
  
  @Override
  protected void onResume() {
    super.onResume();
    mMapView.unpause();
  }

   // Handle callback on committing edits to server
  private class EditCallbackListener implements CallbackListener<FeatureEditResult[][]> {
    private String mOperation = "Operation ";
    private ArcGISFeatureLayer mFeatureLayer = null;
    private boolean mExistingFeature = true;
    private Popup mPopup =null;
    private Context mContext;
    // constant defined for edit result index
    private final static int ADDITION_INDEX = 0;
    private final static int DELETION_INDEX = 1;
    private final static int UPDATE_INDEX = 2;
    
    public EditCallbackListener(Context context, ArcGISFeatureLayer featureLayer, Popup popup, 
        boolean existingFeature, String msg) {
      this.mOperation = msg;
      this.mFeatureLayer = featureLayer;
      this.mExistingFeature = existingFeature;
      this.mPopup = popup;
      this.mContext = context;
    }
    
    @Override
    public void onCallback(FeatureEditResult[][] editResults) {
      if ((mFeatureLayer == null) || (!mFeatureLayer.isInitialized()) 
          || (!mFeatureLayer.isEditable()))
        return;
      
      runOnUiThread(new Runnable() {
        
        @Override
        public void run() {
          Toast.makeText(mContext, mOperation + " succeeded!", Toast.LENGTH_SHORT).show();
        }
      });   
      
      // Similar to featureLayer.applyEdits editResults[0] holds result of feature addition, 
      // while editResults[1] for feature deletion and editResults[2] for feature update
      FeatureEditResult[] deletion = editResults[DELETION_INDEX];
      if ((deletion == null) || (deletion.length <= 0)) {
        // Save attachments to the server if newly added attachments exist.
        // Retrieve object id of the feature
        long oid; 
        FeatureEditResult featureEdit;
        if (mExistingFeature) {
          // The first item in the update edits
          featureEdit = editResults[UPDATE_INDEX][0];
        }  else {
          // The first item in the addition edits
          featureEdit = editResults[ADDITION_INDEX][0];
        }
        oid = featureEdit.getObjectId();
        // prepare oid as int for FeatureLayer
        int objectID = (int) oid;
        
        // Get newly added attachments
        List<File> attachments = mPopup.getAddedAttachments();
        if ((attachments != null) && (attachments.size() > 0)) {
          for (File attachment : attachments) {
            // Save newly added attachment based on the object id of the feature.
            mFeatureLayer.addAttachment(objectID, attachment, new CallbackListener<FeatureEditResult>() {
              @Override
              public void onError(Throwable e) {
                // Failed to save new attachments.
                runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                    Toast.makeText(mContext, "Adding attachment failed!", Toast.LENGTH_SHORT).show();
                  }
                });
              }
              
              @Override
              public void onCallback(FeatureEditResult arg0) {
                // New attachments have been saved.
                runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                    Toast.makeText(mContext, "Adding attachment succeeded!.", Toast.LENGTH_SHORT).show();
                  }
                });
              }
            });
          }
        }
        
        // Delete attachments if some attachments have been mark as delete.
        // Get ids of attachments which are marked as delete.
        List<Integer> attachmentIDs = mPopup.getDeletedAttachmentIDs();
        if ((attachmentIDs != null) && (attachmentIDs.size() > 0)) {
          int[] ids = new int[attachmentIDs.size()];
          for (int i = 0; i < attachmentIDs.size(); i++) {
            ids[i] = attachmentIDs.get(i);
          }
          // Delete attachments
          mFeatureLayer.deleteAttachments(objectID, ids, new CallbackListener<FeatureEditResult[]>() {
            @Override
            public void onError(Throwable e) {
              // Failed to delete attachments
              runOnUiThread(new Runnable() {
                @Override
                public void run() {
                  Toast.makeText(mContext, "Deleting attachment failed!", Toast.LENGTH_SHORT).show();
                }
              });
            }
            
            @Override
            public void onCallback(FeatureEditResult[] objs) {
              // Attachments have been removed.
              runOnUiThread(new Runnable() {
                @Override
                public void run() {
                  Toast.makeText(mContext, "Deleting attachment succeeded!", Toast.LENGTH_SHORT).show();
                }
              });
            }
          });
        }
      
      }
    }

    @Override
    public void onError(Throwable e) {
      runOnUiThread(new Runnable() {
        
        @Override
        public void run() {
          Toast.makeText(mContext, mOperation + " failed!", Toast.LENGTH_SHORT).show();
        }
      });   
    }
    
  }
  
  // Display pop-up fragment on a UI thread.
  private class SimplePopupCreateListener implements PopupCreateListener {
  
     @Override
     public void onResult(final PopupContainer container) {
       if ((container != null) && (container.getPopupCount() > 0)) {
          ((Activity) PopupUICustomizationActivity.this).runOnUiThread(new Runnable() {
  
             @Override
             public void run() {
               if (mPopupFragment == null ) {
                 mPopupFragment = new PopupFragment(mMapView, container);
                 mPopupFragment.show();
               }
             }
         });
       }
     }
     
   }

}




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