Yops
Yops

Reputation: 1

Adding a custom view (linear layout) to the top of a Support Map Fragment is failing

I was working on a project and by following these instructions

(How to add buttons at top of map fragment API v2 layout)

I succeeded on adding components on the top of Map Fragment (Not the Support Map Fragment). After that, I made a change to my code and by following these steps

(SupportMapFragment Instead of MapFragment)

I changed my Map Fragment to Support Map Fragment. I didn't change anything from my map xml, besides

class="com.google.android.gms.maps.MapFragment" 

to

class="com.google.android.gms.maps.SupportMapFragment"

but when I ran my app my linear layout doesn't show on the Map anymore. I tried changing from FrameLayout to RelativeLayout and adding some properties but it seems like it's still overlapping my LinearLayout.

Is there anything missing or in the wrong place?

PS. In the XML Graphical Layout view I can see the linear layout is correctly positioned on the Map.

XML Graphical Layout: http://j52.imgup.net/Imagen156161.png
Screencap of running app: http://i87.imgup.net/Imagen171ef6.png

Here is my current MapFragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:orientation="vertical"
     >

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

    <LinearLayout
        android:id="@+id/box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_margin="10dp"
        android:background="@drawable/button_noborder_background"
        android:orientation="vertical" >

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

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:paddingBottom="5dp"
                android:paddingLeft="5dp"
                android:paddingTop="5dp" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="horizontal" >

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:padding="2dp"
                        android:src="@drawable/icon_marker_inactive" />

                    <TextView
                        android:id="@+id/txtOrigen"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:gravity="center_vertical"
                        android:hint="Origen"
                        android:padding="2dp"
                        android:textAppearance="@android:style/TextAppearance.Small" >
                    </TextView>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="horizontal" >

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:padding="2dp"
                        android:src="@drawable/icon_marker_inactive" />

                    <TextView
                        android:id="@+id/txtDestino"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:gravity="center_vertical"
                        android:hint="Destino"
                        android:padding="2dp"
                        android:textAppearance="@android:style/TextAppearance.Small" >
                    </TextView>
                </LinearLayout>
            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="@drawable/button_blue_background"
                android:gravity="center"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:text="Ruta"
                android:textAppearance="@android:style/TextAppearance.Small"
                android:textColor="@color/white" >
            </TextView>
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

activity_main.xml : My fragments container XML

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!--
         As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions.
    -->

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

    <!--
         android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead.
    -->
    <!--
         The drawer is given a fixed width in dp and extends the full height of
         the container.
    -->

    <fragment
        android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

From MainActivity.java, onNavigationDrawerItemSelected method where I create MapFragment

@Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        Fragment fragment;
        FragmentManager fragmentManager;
        switch (position) {
        default:
        case 0:
            fragment = MapFragment.newInstance(position + 1);
            onSectionAttached(position + 1);
            break;
        }
        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, fragment)
                .commit();

    }

MapFragment.java

import android.app.Activity;
    import android.content.Context;
    import android.location.LocationManager;
    import android.os.Build;
    import android.os.Bundle;
    import android.support.v4.app.FragmentManager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.SupportMapFragment;

    /**
     * A placeholder fragment containing a simple view.
     */
    public class MapFragment extends SupportMapFragment  {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";
        private static View rootView;
        LocationManager locationManager;
        static GoogleMap map;

        /**
         * Returns a new instance of this fragment for the given section number.
         */
        public static SupportMapFragment newInstance(int sectionNumber) {
            SupportMapFragment fragment = new SupportMapFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public MapFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (rootView != null) {
                ViewGroup parent = (ViewGroup) rootView.getParent();
                if (parent != null)
                    parent.removeView(rootView);
            }

            try {
                rootView = inflater.inflate(R.layout.fragment_mapa, container,
                        false);
            }catch(Exception e){

            }

            // Location Service
            locationManager = (LocationManager) getActivity().getSystemService(
                    Context.LOCATION_SERVICE);

            // Inicializacion del mapa
            map = getMapFragment().getMap();
            map.setMyLocationEnabled(true);
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(getArguments().getInt(
                    ARG_SECTION_NUMBER));
        }
        private SupportMapFragment getMapFragment() {
            FragmentManager fm = null;

            // Log.d(TAG, "sdk: " + Build.VERSION.SDK_INT);
            // Log.d(TAG, "release: " + Build.VERSION.RELEASE);

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                // Log.d(TAG, "using getFragmentManager");
                fm = getFragmentManager();
            } else {
                // Log.d(TAG, "using getChildFragmentManager");
                fm = getChildFragmentManager();
            }

            return (SupportMapFragment) fm.findFragmentById(R.id.map);
        }
    }

Upvotes: 0

Views: 2373

Answers (2)

vishnus
vishnus

Reputation: 728

In your activity, keep a reference to your linear layout

LinearLayout root = (LinearLayout) findViewById(R.id.root);

Then after the fragment is added to the layout with fragment transaction, bring your linear layout to front by doing

root.bringToFront();

Upvotes: 1

xiaomi
xiaomi

Reputation: 6703

Into your xml file

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

should be

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

because you initiate into the navigator drawer :

fragmentManager.beginTransaction().replace(R.id.container, fragment)
                .commit();

but there is not id = to container.

Upvotes: 0

Related Questions