I want to implement a navigation drawer, and try to understand how it works. I have tested the navigationDrawerActivity that we can choose in Android Studio with an activity_main as following :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
And looking at the doc Android : http://developer.android.com/intl/es/training/implementing-navigation/nav-drawer.html, the activity_main is :
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
So I don't understand why it's different between android studio and the doc. Can you tell me why is there this difference? And the doc uses fragment, whereas the android studio activity doesn't.
The template used in Android Studio is more recent than the doc which is not updated.
With the new Design Support Library you can use the
NavigationView
inside yourDrawerLayout
to achieve in a very simple way a NavigationDrawer which follows the material guidelines.The only difference is the use of the
NavigationView
instead of a ListView.Of course you can use what you want inside the
DrawerLayout
.The doc just provides a way to demonstrate how simply
DrawerLayout
works, so you will learn how to use basic stuff like creating the drawer, handle navigation clicks and open/close the drawer.The
activity_main
comes from Android Studio is not only that, it also brings some material design to your app. Stuff like theNavigationView
,AppBarLayout
they all come from Android Design Support Library. But the usage ofDrawerLayout
is more or less the same.