I've created a Navigation Drawer project in Android Studio that is ready to go.
Now in one of fragments
, I need a map with a location. How to implement?
I've created a Navigation Drawer project in Android Studio that is ready to go.
Now in one of fragments
, I need a map with a location. How to implement?
I also had this problem!
Here's how I did it:
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>