14

I would like add this xml fragment programmatically to other fragments. Is it possible?

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
Tim
  • 41,901
  • 18
  • 127
  • 145
user3910670
  • 341
  • 1
  • 3
  • 13
  • http://stackoverflow.com/questions/13733299/initialize-mapfragment-programmatically-with-maps-api-v2 – Hulk Apr 17 '15 at 11:29
  • You shouldn't insert a `fragment` inside another `fragment`. It would be better to insert it in a new `activity` through `FragmentTransaction` if you don't want to use the XML layout. – moictab Apr 17 '15 at 11:32

2 Answers2

20

In XML you can add a placeholder container:

<FrameLayout
        android:id="@+id/mapContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

And then in code you can do:

FragmentManager fm = getChildFragmentManager();
SupportMapFragment supportMapFragment =  SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.mapContainer, supportMapFragment).commit();
Numan1617
  • 1,158
  • 5
  • 19
  • can not resolve method : getChildFragmentManager(); also update min api to 17 then how to resolve it ? – Joseph Mekwan Sep 21 '15 at 09:55
  • 2
    @JosephMekwan about getChildFragmentManager(). Just to be clear, if you're trying to insert fragment in Activity, you have to use getSupportFragmentManager() or getFragmentManager(). Where is getChildFragmentManager() used inside fragment if you want to have inner fragment. If you have API 17 or greater it has to be somewhere there. – Yaroslav Havrylovych Dec 01 '16 at 16:59
  • 1
    It's correct, but you have to add "supportMapFragment.getMapAsync(this);" to handle the map and override onMapReady(GoogleMap googleMap) method – Terranology Jul 12 '17 at 12:04
2

Make a layout like:

 <FrameLayout
    android:id="@+id/layout_mapContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_weight="0"
    android:background="@android:color/transparent"
    android:orientation="vertical" >

 <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" />
</FrameLayout>

In Activity declare :

FrameLayout mapLayout = (FrameLayout)findViewById(R.id.layout_mapContainer);

initialise map like this:

private void initialiseMap() {

        FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
        SupportMapFragment mFRaFragment = new MapFragmentActivity();
        mTransaction.add(mapLayout.getId(), mFRaFragment);
        mTransaction.commit();

        try {
            MapsInitializer.initialize(context);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Here MapFragmentActivity is class extends SupportMapFragment

Pankaj
  • 7,908
  • 6
  • 42
  • 65