Download presentation
Presentation is loading. Please wait.
1
Android Developer Fundamentals V2 Lesson 4
Navigation Drawer Lesson 4
2
Navigation Drawer The navigation drawer is a UI panel that shows your app's main navigation menu. It is hidden when not in use, but appears when the user swipes a finger from the left edge of the screen
3
Drawer Layout Added to Android v 22.1
So you need to use Android Support Library Declare your root layout with a DrawerLayout. Inside the DrawerLayout, add a layout for the main content for the UI and another view that contains the contents of the navigation drawer.
4
Drawer Layout <?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity --> <android.support.v4.widget.DrawerLayout xmlns:android=" xmlns:app=" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> </android.support.v4.widget.DrawerLayout>
5
Drawer Layout <?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity --> <android.support.v4.widget.DrawerLayout xmlns:android=" xmlns:app=" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <!-- Layout to contain contents of main body --> <LinearLayout android:orientation="horizontal"></LinearLayout> </android.support.v4.widget.DrawerLayout>
6
Drawer Layout <android.support.v4.widget.DrawerLayout
xmlns:android=" xmlns:app=" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <!-- Layout to contain contents of main body --> <LinearLayout android:orientation="horizontal"></LinearLayout> <!-- Container for contents of drawer --> <android.support.design.widget.NavigationView android:layout_width="wrap_content" android:layout_gravity="start"/> </android.support.v4.widget.DrawerLayout>
7
Menu items for nav drawer
Create a menu resource with the corresponding file name Add menu Items to the resource file <menu xmlns:android=" <group> <item android:title="Add Task" /> android:title="Completed Tasks" /> </group> </menu>
8
Menu items for nav drawer
Configure the menu items listed in the drawer Specify a menu resource with the app:menu attribute <android.support.design.widget.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:background="#003366" />
9
Add a header to the nav drawer
You can add a header at the top of the drawer, by specifying a layout with the app:headerLayout attribute <android.support.design.widget.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:background="#003366"
10
Add a header to the nav drawer
<LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="192dp" android:background="#5599ff" android:gravity="bottom" android:orientation="vertical" android:padding="16dp" <TextView android:layout_height="wrap_content" android:gravity="center" android:text="TO DO APP" android:textSize="20sp" android:textStyle="bold" /> </LinearLayout>
11
Handle navigation click
To receive callbacks, implement the OnNavigationItemSelectedListener interface and attach it to your NavigationView by calling setNavigationItemSelectedListener() NavigationView navigationView = findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { if (menuItem.getTitle().toString().startsWith("Add")) getJob(); } mDrawerLayout.closeDrawers(); return true; });
12
Handle navigation click
To receive callbacks, implement the OnNavigationItemSelectedListener interface and attach it to your NavigationView by calling setNavigationItemSelectedListener() NavigationView navigationView = findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { if (menuItem.getTitle().toString().startsWith("Add")) getJob(); } mDrawerLayout.closeDrawers(); return true; });
13
END
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.