Amey079
Amey079

Reputation: 141

How to display Google Map inside a Fragment's ConstraintLayout

This question has been asked several times but I'm not able to properly add Google Maps(SupportMapFragment) inside my Fragment's ConstraintLayout. My Fragment's ConstrainLayout code is as follows:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    tools:context=".fragments.CountryDetailsFragment">

    <!-- TODO: Update blank fragment layout -->


    <TextView
        android:id="@+id/tv_country_details_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Country Details"
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_country_name_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Name:"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@+id/tv_country_name"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_country_details_label" />

    <TextView
        android:id="@+id/tv_country_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name goes here"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/tv_country_name_label"
        app:layout_constraintTop_toTopOf="@+id/tv_country_name_label" />

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="0dp"
        android:layout_height="300dp"
        android:layout_marginTop="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        tools:layout="@android:layout/simple_gallery_item" />

</androidx.constraintlayout.widget.ConstraintLayout>

When I use the element to add the SupportMapFragment, Android Studio gives me an error saying the FragmentContainerView should be be used instead. Also, if I don't use a dummy layout as I found out from one of the StackOverflow posts, my whole design/blueprint view disappears. What is the best way to add a SupportMapFragment inside my current Fragment's layout? Thanks.

Upvotes: 0

Views: 361

Answers (1)

4gus71n
4gus71n

Reputation: 4111

I think you might have misinterpreted the error. Try changing this:

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="0dp"
        android:layout_height="300dp"
        android:layout_marginTop="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        tools:layout="@android:layout/simple_gallery_item" />

By this:

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="0dp"
        android:layout_height="300dp"
        android:layout_marginTop="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        tools:layout="@android:layout/simple_gallery_item" />

Let me know if it helps you.

Upvotes: 1

Related Questions