You are on page 1of 2

DRAWER MENU

o Change theme to no action bar.

o Create a layout file, change type to toolbar, height is 56 for portrait orientation and 48 hor,
64 for tablets

o Change main layout type (activity main xml) to drawer layout and give it an id.

o Below opening tag include your toolbar


<include layout= “@layout/toolbarname”/> give this tollbar id here

o In main activity declare your tool bar and initialized it using fvbid (oncreate method)

o Inside oncreate setSupportActionBar(toolbarcreatedandinitialized) use if statement to


ensure that you set this if toolbar created is not equal to empty.

o (if toolbar is placed at center (likely) put it on a vertical linear layout (inside main xml).

o Prepare your menu >> along with its icon (try it by setting it up)

o Add navigation view to activity main xml (give it an id)


Width – wrap Height – match
Add created menu inside – app:menu= “menu resorse uri”
Force this line to hide menu  android:layout_gravity="start" otherwise the
bar drawer will always show up

o You can also add header to the navigation view by app:headerLayout=


“@lyout/headerxml”

o You can design headerxml as you design other layouts

o Now lets initialize our Drawer layout inside main java, should not come before toolbar
initialized.

o Then create and initialize instance of ActionBarDrawertogle like below, along wth its plug
ActionBarDraweTogle instance = new ActionBarDrawerTogle(Activityname.this,
drawerlayoutCreted, toolbarCreated, “OPEN TEXT FOR R.sting”, “CLOSE TEXT FOR
R.string”, )

{
//this is a plug
//override onDrawerOpened

//override onDrawerClosed

After plug

o Add drawer listener to the drawerlyout created and pass actionbardrawertoggle instance
as parameters
Drawelayoutname.addDraweListener(instance);

o Belowit syncstate of actionbardrawertoggle instance

o Example; actionbardrawertoggle.syncState.

ADDING LISTENER TO THE DRAWER LAYOUT

o Declare and initialize your navigation view

Referencing declared navigation view set setNavigationItemSelectedListener

o Inside overrided (onNavigationItemSelected) close the drawer (as once


item is clicked)
Drawerlayoutname.closeDrawers();

o Create switch statement that switch parameterized MenuItem item


(item.getItemId())

o Cases (case) should be Id of menus.

You might also like