0

I'm running google maps API V2 as FragmentActivity.

May activity layout is here: activity_landing.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_gravity="center">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

PROBLEM: I want to dynamically add a fragment to this layout FrameLayout.

Here is my activity code:

BuildingPageFragment buildingFragment = new BuildingPageFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, buildingFragment, "buildingPageFragment");
fragmentTransaction.commit();

Here is the fragments layout that I am trying to add the the FrameLayout in activity_landing.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
android:background="#FFBB00">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="BUILDING PAGE"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

The Fragment is not shoing on top of the to the fragment in the activity_landing.xml.

I am new to android dev so what am I missing? Thanks

KasparTr
  • 2,328
  • 5
  • 26
  • 55
  • Do you wanna `add another fragment` on the map fragment or `instead of map fragment`? Better to here a screenshot to explain this – bjiang Aug 03 '15 at 17:35
  • I want to add another fragment on top of the map. About the screenshot, I need to know how to construct the activity class xml layout. FrameLayout is not helping me since I dont have the Relative or LinearLayout tags there to put it in. Adding those tags crashes the app. Thanks for the question! – KasparTr Aug 04 '15 at 07:19
  • I updated the question and showed the changes I have done in the meanwhile. – KasparTr Aug 04 '15 at 08:22
  • I found that your code seems to be upside down. Exchange line `FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();` and `fragmentTransaction.add(R.id.fragment_container, buildingFragment, "buildingPageFragment");` – bjiang Aug 04 '15 at 16:11
  • How can you add to FragmentTransaction object before you declare it as FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();? All examples online also use this kind of order. – KasparTr Aug 05 '15 at 09:15
  • [SOLVED]: I just needed to put the FrameLayout as a parent XML tag in activity_landing.xml. – KasparTr Aug 05 '15 at 09:53
  • Oh.. I should aware of that, cuz I answered the similar question [here](http://stackoverflow.com/a/28326783/4186942):) Thanks for the reminder. – bjiang Aug 05 '15 at 16:31

0 Answers0