HERE Android SDK Developer's Guide

Adding a MapFragment at Runtime

Earlier tutorials in this document featured adding a MapFragment to an activity by editing the layout XML file. You can also add a MapFragment to an activity dynamically, during runtime, by performing the following steps in the Activity class:

  1. Create a layout container:
    
    final int CONTAINER_ID = 1234567;
    LinearLayout layoutContainer = new LinearLayout(this);
    layoutContainer.setOrientation(LinearLayout.HORIZONTAL);
    layoutContainer.setId(CONTAINER_ID);
    
  2. Define a map tag:
    
    final String MAP_TAG = "map_tag";
    
  3. Create a map fragment and add it using the fragment manager:
    
    mapFragment = new MapFragment();
    getFragmentManager().beginTransaction().add(layoutContainer.getId(), mapFragment, MAP_TAG).commit();
    
  4. Initialize the map fragment by implementing OnEngineInitListener:
    
    MyOnEngineInitListener onEngineInitListener = new MyOnEngineInitListener();
    ApplicationContext context = new ApplicationContext(this);
    mapFragment.init(context, onEngineInitListener);
    
  5. Finally, show the content view:
    
    setContentView(layoutContainer);
    

For more information on adding a fragment at runtime, see this article: https://developer.android.com/training/basics/fragments/fragment-ui.html#AddAtRuntime.