Various ways of creating android navigation drawer - Cont'd
The other method of creating the navigation drawer comes inbuilt as a startup templaetewith the latest version of android studio, this method does not require instantiating a fragment which makes it preferable for me. It uses a navigation view widget as illustrated below
activity_main.xml - Unlike the one with the fragment, this contain a top level drawer layout then the main app bar and navigation view widget
Files
Github link: https://github.com/devDeji/ActivityNavactivity_main.xml - Unlike the one with the fragment, this contain a top level drawer layout then the main app bar and navigation view widget
<android.support.v4.widget.DrawerLayout tools:openDrawer="start"> <include layout="@layout/app_bar_main" /> Note: An header resource is also included in the nav drawer <android.support.design.widget.NavigationView app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.DrawerLayout>
app_bar_main.xml - This file contains the toolbar, the top level coordinator layout and
inclusive of a content menu
<android.support.design.widget.CoordinatorLayout> <android.support.design.widget.AppBarLayout > <android.support.v7.widget.Toolbar /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton app:srcCompat="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>MainActivity.java - Here the ActionBarDrawerToggle API binds the navigation drawer layout tothe toolbar, then instantiating the navigation view widget to add a listnerDrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this);Note: To use a custom action bar, you need to set the app theme in the style resource file tonoActionBar then include an action bar in the activity_main.xml file.Hope you find this piece of article interesting and gained enough insight to creating anavigation drawer in android studio
Comments
Post a Comment