tim4dev
tim4dev

Reputation: 2987

Layout with two fragments: one fragment and a fragment with the Google Map

I'm trying to implement the following design. TextView should take up space by content, and the rest of the space is a map.

+----------------+
|                |
|    Map         | <-- id/fragment_container_1
|                |
|                |
+----------------+
|                |
|    TextView    | <-- id/fragment_container_2
|                |
+----------------+

main.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <FrameLayout
        android:id="@+id/fragment_container_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

fragment_1.xml

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Client Label" />
</LinearLayout>

map.xml

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.gms.maps.MapView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map_view_observer" />
</LinearLayout>

Java code:

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// map
Fragment fragmentMap = fragmentManager.findFragmentByTag("ObserverMapFragment");
if (fragmentMap == null)   {
    fragmentMap = ObserverMapFragment.newInstance();
    fragmentTransaction
            .replace(R.id.fragment_container_1, fragmentMap, "ObserverMapFragment");
}
// fargment 1
Fragment fragmentObserver = fragmentManager.findFragmentByTag("ObserverFragment");
if (fragmentObserver == null)   {
    fragmentObserver = ObserverFragment.newInstance();
    fragmentTransaction
            .replace(R.id.fragment_container_2, fragmentObserver, "ObserverFragment");
}
fragmentTransaction.commit();

However, after that I get that the map occupies all visible space.

+----------------+
|                |
|                |
|    Map         |
|                |
|                |
+----------------+

Where I made a mistake? Maybe I'm wrong in the very architecture of the application with several fragments?

Upvotes: 1

Views: 2100

Answers (3)

Kevan Aghera
Kevan Aghera

Reputation: 241

Don't give a textview size and map size match parent.

Upvotes: -1

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

You can try with this

  1. Add android:layout_height="0dp"
  2. Add android:weightSum

Finally

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="2">

            <FrameLayout
                android:layout_weight="1"
                android:id="@+id/fragment_container_1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
              />

            <FrameLayout
                android:layout_weight="1"
                android:id="@+id/fragment_container_2"
                android:layout_width="match_parent"
                android:layout_height="0dp" />
      </LinearLayout>

Upvotes: 2

Shubham Goel
Shubham Goel

Reputation: 2186

Edit your main.xml like this

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
    android:layout_weight="1"
    android:id="@+id/fragment_container_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

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

Upvotes: 2

Related Questions