Hi Michail,
The followings are some example code snippets to work with our API to display a simple popup in an Android app. The simple popup will only show title and attributes. If your popup configuration includes media and attachment, you need to add more logics into your app to display these elements in your popup. I also include some links to a few classes of our Android SDK that you may want to check out. You may also want to take a look at a .Net open source app called Data Collection for .Net which displays popups in some workflows. Though it is a .Net app the API patterns and workflows supported by our Runtime Android SDK and .Net SDK are very similar. Hopeful these can get you started.
Links:
https://developers.arcgis.com/android/latest/api-reference/reference/com/esri/arcgisruntime/mapping/...
https://developers.arcgis.com/android/latest/api-reference/reference/com/esri/arcgisruntime/mapping/...
https://developers.arcgis.com/android/latest/api-reference/reference/com/esri/arcgisruntime/mapping/...
https://developers.arcgis.com/android/latest/api-reference/reference/com/esri/arcgisruntime/mapping/...
Code snippets:
1. Use identify to obtain popups from your web map
mMapView.setOnTouchListener(new MyTouchListener(this, mMapView));
private class MyTouchListener extends DefaultMapViewOnTouchListener {
private MapView mapView;
private Context context;
public MyTouchListener(Context context, MapView mapView) {
super(context, mapView);
this.context = context;
this.mapView = mapView;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if (mapView == null ) {
return super.onSingleTapConfirmed(e);
}
android.graphics.Point screenPoint = new android.graphics.Point((int) e.getX(), (int) e.getY());
doIdentify(mapView, screenPoint);
return true;
}
private void doIdentify(final MapView mapView, final android.graphics.Point screenPoint) {
final ListenableFuture<List<IdentifyLayerResult>> future = mapView.identifyLayersAsync(
screenPoint, 10.0, false, 3);
future.addDoneListener(new Runnable() {
@Override public void run() {
try {
List<IdentifyLayerResult> results = future.get();
if ((results == null) || (results.isEmpty())) {
Log.i(TAG, "Null or empty result from identify. ");
return;
}
IdentifyLayerResult result = results.get(0);
List<Popup> popups = result.getPopups();
if ((popups == null) || (popups.isEmpty())) {
Log.i(TAG, "Null or empty popup from identify. ");
}
Popup popup = popups.get(0);
createPopupView(popup);
} catch (Exception ex) {
Log.i(TAG, "exception in identify: " + ex.getMessage());
}
}
});
}
}
private void createPopupView(Popup popup) {
SimplePopupFragment fragment = SimplePopupFragment.newInstance();
fragment.setPopupManager(SimplePopupActivity.this, popup);
getSupportFragmentManager().beginTransaction().
replace(android.R.id.content, fragment, "popup fragment")
.addToBackStack("popup fragment")
.commit();
}
2. Show popup elements in a fragment
public class SimplePopupFragment extends Fragment {
private PopupManager mPopupManager;
private static final String TAG = SimplePopupFragment.class.getSimpleName();
public static SimplePopupFragment newInstance() {
SimplePopupFragment fragment = new SimplePopupFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (!checkPopupManager()) {
return null;
}
View view = inflater.inflate(R.layout.simple_popup_view, container, false);
initPopupView(view);
return view;
}
public void setPopupManager(Context context, Popup popup) {
mPopupManager = new PopupManager(context, popup);
}
private boolean checkPopupManager() {
if (mPopupManager == null) {
Log.w(TAG, "Need to set PopupManager before showing the popup view!");
return false;
}
return true;
}
private void initPopupView(View popupView) {
if (mPopupManager == null) {
return;
}
initTitleView(popupView);
initAttributeView(popupView);
}
private void initTitleView(View popupView) {
TextView titleView = (TextView) popupView.findViewById(R.id.simple_titleview_title);
titleView.setText(mPopupManager.getTitle());
TextView editInfoView = (TextView) popupView.findViewById(R.id.simple_titleview_editinfo);
editInfoView.setText(mPopupManager.getEditSummary());
}
private void initAttributeView(View popupView) {
RecyclerView attrView = (RecyclerView) popupView.findViewById(R.id.simple_attribute_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
attrView.setLayoutManager(linearLayoutManager);
if ((mPopupManager.getCustomHtmlDescription() == null) || (mPopupManager.getCustomHtmlDescription().length()== 0)) {
WebView webView = (WebView) popupView.findViewById(R.id.simple_description_view);
webView.setVisibility(View.GONE);
attrView.setVisibility(View.VISIBLE);
attrView.setHasFixedSize(true);
SimpleAttributeAdapter adapter = new SimpleAttributeAdapter(mPopupManager);
attrView.setAdapter(adapter);
} else {
attrView.setVisibility(View.GONE);
WebView webView = (WebView) popupView.findViewById(R.id.simple_description_view);
webView.loadData(mPopupManager.getCustomHtmlDescription(), "text/html; charset=utf-8", null);
webView.setVisibility(View.VISIBLE);
}
}
}
3. Adapter of attributes
public class SimpleAttributeAdapter extends RecyclerView.Adapter<SimpleAttributeAdapter.ViewHolder> {
private static PopupManager mPopupManager;
public SimpleAttributeAdapter(PopupManager popupManager) {
mPopupManager = popupManager;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.simple_attribute_row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (mPopupManager == null) {
return;
}
PopupField field = mPopupManager.getDisplayedFields().get(position);
holder.getValueView().setText(mPopupManager.getFormattedValue(field));
if ((mPopupManager.getFieldLabel(field) == null)
|| (mPopupManager.getFieldLabel(field).length() == 0)) {
holder.getLableView().setText(field.getFieldName());
} else {
holder.getLableView().setText(mPopupManager.getFieldLabel(field));
}
}
@Override
public int getItemCount() {
int count = 0;
if (mPopupManager != null) {
count = mPopupManager.getDisplayedFields().size();
}
return count;
}
@Override public int getItemViewType(int position) {
return position;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView mLabelView;
private TextView mValueView;
public ViewHolder(View itemView) {
super(itemView);
mLabelView = (TextView) itemView.findViewById(R.id.popup_attributeview_fieldlabel);
mValueView = (TextView) itemView.findViewById(R.id.popup_attributeview_fieldvalue);
}
public TextView getLableView() {
return mLabelView;
}
public TextView getValueView() {
return mValueView;
}
}
}
4. Layout of popup fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fillViewport="true"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/simple_titleview_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="13dp"/>
<TextView
android:id="@+id/simple_titleview_editinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:textColor="@android:color/darker_gray"
android:textSize="11dp"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/simple_attribute_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
<WebView
android:id="@+id/simple_description_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible">
</WebView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
5. Layout of an attribute row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/popup_attributeview_fieldlabel"
android:textSize="15dp"
android:textColor="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/popup_attributeview_fieldvalue"
android:textSize="15dp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>