Inflate multiple views to display multiple MapFragments

58 views Asked by At

I have an xml layout which includes the following code:

<fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

This layout can be inflated in the main activity more than once and, in that case, should display multiple MapFragments. When that happens, however, I get the following error:

Duplicate id 0x7f0a0213, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment

From my understanding, this happens because you can't have more than 1 <fragment> with the same id.

How can I inflate these views with these map fragments multiple times? Is it possible to assign different ID's to each fragment every time the layout is inflated?

EDIT: I found this. I guess I can't set the ID of the fragment itself programmatically.

1

There are 1 answers

3
Jack On

just change the id from android:id="@+id/map" to android:id="@+id/map2" and etc. Please take into account, that every google map is expensive for performance and memory, so ask yourself do you really need more than one map