Thanassis
Thanassis

Reputation: 877

How to include FragmentActivity layout in Activity layout?

I want to include a FragmentActivity layout in another AppCompatActivity layout.

In particular I want to show a Google map (in this case the FragmentActivity) inside a container of the other AppCompatActivity . i am going to show my code.

1. The AppCompatActivity layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/map_toolbar"
        layout="@layout/app_bar" />

    <LinearLayout
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>
</LinearLayout>

2.The AppCompatActivity class

public class MapActivity extends AppCompatActivity {

    private Toolbar _toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        _toolbar = (Toolbar) findViewById(R.id.map_toolbar);
        setSupportActionBar(_toolbar);
    }
}

3.The FragmentActivity layout:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

4.The FragmentActivity class:

public class MapFragmentActivity extends FragmentActivity implements OnMapReadyCallback
{

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }
}

When I run this just show the app bar on top and blank frame in the place that I needed to have the map.

Please help.

thanks.

Upvotes: 3

Views: 3323

Answers (2)

spike_stack
spike_stack

Reputation: 729

I don't see your map layout inside your MapActivity. Furthermore you are using your map in a FragmentActivity not a Fragment. This is how I would do it.

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
    private Toolbar _toolbar;
    private GoogleMap mGoogleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        _toolbar = (Toolbar) findViewById(R.id.map_toolbar);
        setSupportActionBar(_toolbar);

        initMap();
    }

    public void initMap() {
        MapFragment map = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        map.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        try {
            if (googleMap != null) {
                mGoogleMap = googleMap;                             
                mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);               
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("ERROR", "GOOGLE MAPS NOT LOADED");
        }
    }
}

Then I would do this in my XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/map_toolbar"
        layout="@layout/app_bar" />

    <LinearLayout
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

    </LinearLayout>
</LinearLayout>

Then you don't even need the MapFragmentActivity as its a bit redundant. Also you are now waiting for the map to be ready before you display it with the implements OnMapReadyCallback. This will save you some headaches later with inflating the fragment before its ready.

Upvotes: 4

Paresh
Paresh

Reputation: 6857

Place the MapFragment into the Layout of AppCompatActivity instead.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/map_toolbar"
            layout="@layout/app_bar" />

        <LinearLayout
            android:id="@+id/map_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
               <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/map"
                    android:name="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
        </LinearLayout>
    </LinearLayout>

Upvotes: 1

Related Questions