AnsweredAssumed Answered

Cannot use MapView in a LinearLayout

Question asked by stephen_quan on Jun 25, 2012
Latest reply on Nov 28, 2012 by JFA0427
I am having two issues when I place a MapView control in a LinearLayout (i.e. useLinearLayout = true):

1. The MapView no longer renders properly. It renders black. If a show a popup Toast, the MapView renders temporarily then goes black again.
2. The MapView no longer generates OnSingleTapListener() events.

If I setContentView() on the MapView control, everything works fine (i.e. useLinearLayout = false).

The reason why I want to use a LinearLayout is I want to add other UI controls to my Activity, such as a Button.

The following code demonstrate the above symptoms.
It is based on the Hello World Map sample.
By setting useLinearLayout to true or false will cause the either of the above two symptoms to occur.

I get these symptoms on both SDK 1.0.1 and SDK 2.0.0.

package com.esri.android.editdemo;  import com.esri.android.map.GraphicsLayer; import com.esri.android.map.MapView; import com.esri.android.map.ags.ArcGISTiledMapServiceLayer; import com.esri.android.map.event.OnSingleTapListener; import com.esri.core.geometry.Envelope; import com.esri.core.geometry.GeometryEngine; import com.esri.core.geometry.Point; import com.esri.core.geometry.SpatialReference; import com.esri.core.map.Graphic; import com.esri.core.symbol.SimpleMarkerSymbol;  import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast;  public class EditDemoActivity extends Activity {  LinearLayout linearLayout = null;  MapView map = null;  GraphicsLayer graphicsLayer = null;  SpatialReference webMercator = SpatialReference.create(102100);    public void onCreate(Bundle savedInstanceState)  {   super.onCreate(savedInstanceState);    final Context context = this;      boolean useLinearLayout = true; // <-- mapView fails, but has button   // boolean useLinearLayout = false; // <-- mapView works, but no button   if (useLinearLayout)   {    linearLayout = new LinearLayout(this);    linearLayout.setOrientation(LinearLayout.VERTICAL);    setContentView(linearLayout);    Button testButton = new Button(this);    testButton.setText("Hello");    testButton.setOnClickListener(new OnClickListener()    {     public void onClick(View v)     {      Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();     }    });    linearLayout.addView(testButton);    map = new MapView(this);    linearLayout.addView(map);   }   else   {    map = new MapView(this);    setContentView(map);   }      // Add the ArcGIS Online layer to the map.   map.addLayer(new ArcGISTiledMapServiceLayer("" +   "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"));   map.setOnSingleTapListener(new OnSingleTapListener()   {    public void onSingleTap(float x, float y)    {     Point newPoint = map.toMapPoint(new Point(x, y));     SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(Color.RED, 20, SimpleMarkerSymbol.STYLE.CIRCLE);     graphicsLayer.addGraphic(new Graphic(newPoint, simpleMarkerSymbol));    }   });     // Set the extent to ESRI Redlands Campus   Point minPoint = GeometryEngine.project(-117.200, 34.057, webMercator);   Point maxPoint = GeometryEngine.project(-117.198, 34.059, webMercator);   Envelope extent = new Envelope(minPoint.getX(), minPoint.getY(), maxPoint.getX(), maxPoint.getY());   extent.inflate(10000, 10000);   map.setExtent(extent);    // Create a graphics layer and add it to the map.   graphicsLayer = new GraphicsLayer();   map.addLayer(graphicsLayer);  }   protected void onPause()  {   super.onPause();   map.pause();  }   protected void onResume()  {   super.onResume();   map.unpause();  }  }

Outcomes