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
*//www.java2s.com
* 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.maplegend;
import android.app.Activity;
import android.app.DialogFragment;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISLayerInfo;
import com.esri.core.map.Legend;
import java.util.Collections;
import java.util.List;
/**
* A dialog that shows the legend of a ArcGISDynamicMapServiceLayer.
*/publicclass LegendDialogFragment extends DialogFragment {
publicstaticfinal String TAG = LegendDialogFragment.class.getSimpleName();
LinearLayout mLinearLayout;
private ArcGISDynamicMapServiceLayer mLayer;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mLinearLayout = (LinearLayout) inflater.inflate(R.layout.legend_dialog_fragment_layout, null);
getDialog().setTitle(getActivity().getString(R.string.legend));
mLayer = ((MainActivity) getActivity()).getLayer();
// before we can show the legend we have to fetch the legend info asynchronously
new FetchLegendTask().execute();
return mLinearLayout;
}
publicvoid onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof MainActivity)) {
thrownew IllegalStateException("Hosting activity needs to be of type MainActivity");
}
}
/**
* Populates the list of legend items for each sub-layer of an ArcGISDynamicMapServiceLayer.
*/privateclass LayerLegendAdapter extends BaseAdapter {
privatefinal List<Legend> mLegends;
public LayerLegendAdapter(List<Legend> legends) {
mLegends = legends != null ? legends : Collections.EMPTY_LIST;
}
publicint getCount() {
return mLegends.size();
}
public Object getItem(int position) {
return mLegends.get(position);
}
publiclong getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = getActivity().getLayoutInflater().inflate(R.layout.layer_legend_item_layout, null);
}
Legend legend = mLegends.get(position);
TextView textView = (TextView) view;
textView.setText(legend.getLabel());
Bitmap bitmap = legend.getImage();
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
textView.setCompoundDrawables(drawable, null, null, null);
return view;
}
}
/**
* Retrieves the legend information asynchronously from the ArcGISDynamicMapServiceLayer.
*/privateclass FetchLegendTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
mLayer.retrieveLegendInfo();
return null;
}
@Override
protectedvoid onPostExecute(Void result) {
super.onPostExecute(result);
for (ArcGISLayerInfo layerInfo : mLayer.getLayers()) {
View view = getActivity().getLayoutInflater().inflate(R.layout.layer_legend_layout, null);
populateLegendView(view, layerInfo);
mLinearLayout.addView(view);
}
}
private View populateLegendView(View view, ArcGISLayerInfo layerInfo) {
if (layerInfo != null) {
TextView textView = (TextView) view.findViewById(R.id.layer_legend_title_textview);
ListView listView = (ListView) view.findViewById(R.id.layer_legend_symbols_listview);
textView.setText(layerInfo.getName());
listView.setAdapter(new LayerLegendAdapter(layerInfo.getLegend()));
}
return view;
}
}
}