Search code examples
c#androidxamarinarcgisesri

Xamarin - Add ESRI MapView dynamically to FrameLayout


I haven't found any ability to add MapViewer from ArcGIS SDK for Xamarin using axml definition. This doesn't seem to be possible:

<?xml versio="1.0" encoding="utf-8"?>
<LinearLayout>
   ...
   <com.esri.mapping.MapView ... />
</LinearLayout>

I found examples where entire view is constructed dynamically using code. https://github.com/Esri/arcgis-runtime-samples-xamarin/tree/master/src/Android/Xamarin.Android

I would prefer to use XML to have more flexibility to extend the view so I had an idea to add MapView dynamically into FrameLayout - define resource like this (activity_map.axml):

<?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="match_parent">
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mapView" />
</LinearLayout>

And then in the activity class:

public class MainActivity : MvxActivity<MainViewModel>
{
    private MapView _mapView = new MapView();

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        _mapView = new MapView(this);

        SetContentView(Resource.Layout.activity_amp);
        //How to get FrameLayout id ???
        var mapContainer=(FrameLayout)FindViewById();
        mapContainer.AddView(_mapView);
    }
}

Is this a valid approach? Would you recommend another one?


Solution

  • <?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="match_parent">
        <Esri.ArcGISRuntime.UI.Controls.MapView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/mapView" />
    </LinearLayout>
    

    and

    public class MainActivity : MvxActivity<MainViewModel>
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            SetContentView(Resource.Layout.activity_main);
            var mapView = (MapView) FindViewById(Resource.Id.mapView);
            mapView.Map = ViewModel.Map;
        }   
    }