How to Implement Navigation Drawer with Map in a Fragment

Asked

Viewed 680 times

3

I created a project Navigation Drawer in Android Studio the one that comes ready.

Now on one of fragments, need a map with a location. How to implement?

1 answer

5


I had this problem too!

Follow below as I’ve managed:

Activity:

 public class MapsActivity extends AppCompatActivity  {


        private DrawerLayout mDrawerLayout;
        private ListView mListDrawer;
        private ActionBarDrawerToggle mToggle;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity);
            mDrawerLayout = DrawerLayout.class.cast(findViewById(R.id.mDrawerLayout));
            mListDrawer = ListView.class.cast(findViewById(R.id.mListDrawer));


             mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.title_menu_open, R.string.title_menu_close)
                {
                    @Override
                    public void onDrawerClosed(View drawerView) {
                        super.onDrawerClosed(drawerView);
                        getSupportActionBar().setTitle(R.string.title_menu_open);
                    }
                    @Override
                    public void onDrawerOpened(View drawerView) {
                        super.onDrawerOpened(drawerView);
                        getSupportActionBar().setTitle(R.string.title_menu_close);
                    }
                };

                mDrawerLayout.setDrawerListener(mToggle);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);

                openFragment(new MapFragment());
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            mToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mToggle.onConfigurationChanged(newConfig);
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            return false;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (mToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
        @Override
        protected void onResume() {
            super.onResume();
        }


        public void openFragment(final Fragment fragment){
            if(null == fragment) return;
            final FragmentManager mManager =  getFragmentManager();
            if(mDrawerLayout != null){
                mDrawerLayout.closeDrawers();
            }
            if(null != mManager)
            {
                FrameLayout.class.cast(findViewById(R.id.mContent)).removeAllViewsInLayout();
                mManager.beginTransaction().replace(R.id.mContent, fragment).commit();
            }
        }


    }

Fragment:

public class MapFragment extends Fragment
 {


    MapView mMapView;

    GoogleMap mMap;



    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.activity_maps, container, false);
        mMapView = MapView.class.cast(view.findViewById(R.id.mapView));
        mMapView.onCreate(savedInstanceState);

        if (mMapView != null) {
            mMap = mMapView.getMap();
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMap.getUiSettings().setAllGesturesEnabled(true);
            mMap.getUiSettings().setCompassEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(true);

        }
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
}

Activity.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mDrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:clipToPadding="true"
    tools:context=".MapsActivity" >

    <FrameLayout
        android:fitsSystemWindows="true"
        android:clipToPadding="false"
        android:id="@+id/mContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView android:id="@+id/mListDrawer"
        android:fitsSystemWindows="true"
        android:clipToPadding="false"
        android:layout_width="165dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"/>

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

activity_maps.xml:

<RelativeLayout
    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">


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

</RelativeLayout>
  • Thanks guy for the help.... (y)

  • If the answer offered you what you wanted, please mark it as correct! Sweats

  • @Thiagoluizdomacoski found your answer too vague, not complete, and what about the implementation of Androidmanifest.xml and the Google Maps API? and the key from Google Developers, Keytool with the md5? hash and dependencies there in Gradle?

  • 2

    It’s because the question is how to implement a Drawer with a map in a Fragment. Not how to configure the map itself. If the user marked as correct, maybe the answer has solved the question! If you wanted, ask a specific question, how to configure the map. Thanks for the tip!

  • @Thiagoluizdomacoski was worth dear, now I understand, but remember that the whole implementation will only work if you are with everything ok, the dependencies, the key and Androidmanifest.xml configured correctly, and really this of configuring, gives basis for a new question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.