Site icon C1CTech

Android Navigation Drawer example

<p>As you know lot of android applications introduced a sliding panel menu to navigate between major modules of the application&period; Previously this kind of UI was done using some third party libraries where a list view and some swiping gestures used to achieve this&period; But now android  introduced sliding panel menu by introducing a newer concept called <strong><a class&equals;"link" href&equals;"http&colon;&sol;&sol;developer&period;android&period;com&sol;design&sol;patterns&sol;navigation-drawer&period;html" target&equals;"&lowbar;blank" rel&equals;"nofollow noopener">Navigation Drawer<&sol;a><&sol;strong> in which we combine <strong><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;widget&sol;DrawerLayout&period;html" target&equals;"&lowbar;blank" rel&equals;"nofollow noopener">DrawerLayout<&sol;a><&sol;strong> and <strong><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;design&sol;widget&sol;NavigationView&period;html" target&equals;"&lowbar;blank" rel&equals;"nofollow noopener">NavigationView<&sol;a><&sol;strong> to achieve the desired output&period;<&sol;p>&NewLine;<p>The navigation drawer is a UI panel that shows your app&&num;8217&semi;s main navigation menu&period; Most of the time Sliding Menu &lpar;Navigation Drawer&rpar; will be hidden and can be shown by swiping the screen from left edge to right or tapping the app icon on the action bar&period;<&sol;p>&NewLine;<p>In this tutorial I will show you how to implement a navigation drawer using the <strong><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;widget&sol;DrawerLayout&period;html">DrawerLayout<&sol;a><&sol;strong> APIs available in the <a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;tools&sol;support-library&sol;index&period;html"><strong>Support Library<&sol;strong><&sol;a>&period;<&sol;p>&NewLine;<p>Get <span style&equals;"color&colon; &num;000080&semi;"><strong>GITHUB<&sol;strong><&sol;span> code from <a href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;NavigationDrawerExample"><span style&equals;"color&colon; &num;00ff00&semi;"><strong>here<&sol;strong><&sol;span><&sol;a>&period;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"muL31F3U35w" title&equals;"Android Navigation Drawer Example"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;muL31F3U35w"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;muL31F3U35w&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Navigation Drawer Example"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">Create new Project<&sol;span><&sol;h3>&NewLine;<p><strong>1<&sol;strong>&period; Create a new project in <span style&equals;"color&colon; &num;008000&semi;"><strong>Android Studio<&sol;strong><&sol;span> from <span style&equals;"color&colon; &num;008000&semi;"><strong>File &Implies; New Project<&sol;strong><&sol;span> by filling the required details&period; When it prompts you to select the activity&comma; choose <span style&equals;"color&colon; &num;008000&semi;"><strong>Empty Activity<&sol;strong><&sol;span> and continue&period;<&sol;p>&NewLine;<p><strong>2<&sol;strong>&period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span> located in app level and add below dependencies&period; These dependencies are needed for navigation drawer&period;<&sol;p>&NewLine;<pre><code>dependencies &lbrace;&NewLine; implementation fileTree&lpar;dir&colon; 'libs'&comma; include&colon; &lbrack;'&ast;&period;jar'&rsqb;&rpar;&NewLine; implementation 'com&period;android&period;support&colon;appcompat-v7&colon;27&period;1&period;1'&NewLine; implementation 'com&period;android&period;support&colon;design&colon;27&period;1&period;1'&NewLine; implementation 'com&period;android&period;support&colon;support-v4&colon;27&period;1&period;1'&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">Creating Fragments &amp&semi; Activities<&sol;span><&sol;h3>&NewLine;<p><strong>3<&sol;strong>&period; Create  package  <span style&equals;"color&colon; &num;008000&semi;"><strong>fragment<&sol;strong><&sol;span> which contains all the fragment and leave <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> in root package&period;<&sol;p>&NewLine;<p><strong>4&period;<&sol;strong>&period; Create all the fragment classes needed for navigation menu&period; Overall I am creating five fragment classes&period; <strong><span style&equals;"color&colon; &num;008000&semi;">Right<&sol;span> <span style&equals;"color&colon; &num;008000&semi;">click<&sol;span><&sol;strong> on <span style&equals;"color&colon; &num;008000&semi;"><strong>fragment<&sol;strong><&sol;span> package&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">New<&sol;span> &Implies; <span style&equals;"color&colon; &num;008000&semi;">Fragment<&sol;span> &Implies; <span style&equals;"color&colon; &num;008000&semi;">Fragment &lpar;Blank&rpar;<&sol;span><&sol;strong> and name it as <span style&equals;"color&colon; &num;008000&semi;"><strong>HomeFragment&period;java<&sol;strong><&sol;span>&period; This fragment will be loaded always whenever user open the app&period;<&sol;p>&NewLine;<p>Likewise create other fragments named <span style&equals;"color&colon; &num;008000&semi;"><strong>ImportFragment<&sol;strong>&comma; <strong>GalleryFragment<&sol;strong>&comma; <strong>SlideshowFragment<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>ToolsFragment<&sol;strong><&sol;span> &period;<&sol;p>&NewLine;<p><strong>5<&sol;strong>&period; In our navigation drawer menu&comma; there are two other menu items&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>SendActivity<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>ShareActivity<&sol;strong><&sol;span>&period; For these two we are gonna create activities instead of fragments&period; Create new two <span style&equals;"color&colon; &num;008000&semi;"><strong>activities<&sol;strong><&sol;span> named <span style&equals;"color&colon; &num;008000&semi;"><strong>SendActivity&period;java<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>ShareActivity&period;java<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">Adding Navigation Drawer<&sol;span><&sol;h3>&NewLine;<p><strong>6<&sol;strong>&period; Under <strong><span style&equals;"color&colon; &num;008000&semi;">res<&sol;span> &Implies; <span style&equals;"color&colon; &num;008000&semi;">menu<&sol;span><&sol;strong> directory&comma; create a menu xml file named <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&lowbar;drawer&period;xml<&sol;strong><&sol;span>&period; This menu renders the Navigation Drawer list items&period; Here we set the <span style&equals;"color&colon; &num;008000&semi;"><strong>icons<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>labels<&sol;strong><&sol;span>&period; You can also notice here <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;group&gt&semi;<&sol;strong><&sol;span>is used to combine multiple items under single levels&period; An <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;item&gt&semi;<&sol;strong><&sol;span> also can be used to group multiple child items with a title&period; This provides a horizontal separator between the two sets&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&lowbar;drawer&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;menu xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; tools&colon;showIn&equals;"navigation&lowbar;view"&gt&semi;&NewLine;&NewLine; &lt&semi;group android&colon;checkableBehavior&equals;"single"&gt&semi;&NewLine; &lt&semi;item&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;nav&lowbar;home"&NewLine; android&colon;icon&equals;"&commat;drawable&sol;home2"&NewLine; android&colon;title&equals;"Home"&NewLine;&NewLine; &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;item&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;nav&lowbar;import"&NewLine; android&colon;icon&equals;"&commat;drawable&sol;ic&lowbar;menu&lowbar;import"&NewLine; android&colon;title&equals;"Import"&NewLine;&NewLine; &sol;&gt&semi;&NewLine; &lt&semi;item&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;nav&lowbar;gallery"&NewLine; android&colon;icon&equals;"&commat;android&colon;drawable&sol;ic&lowbar;menu&lowbar;gallery"&NewLine; android&colon;title&equals;"Gallery" &sol;&gt&semi;&NewLine; &lt&semi;item&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;nav&lowbar;slideshow"&NewLine; android&colon;icon&equals;"&commat;android&colon;drawable&sol;ic&lowbar;menu&lowbar;slideshow"&NewLine; android&colon;title&equals;"Slideshow" &sol;&gt&semi;&NewLine; &lt&semi;item&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;nav&lowbar;tools"&NewLine; android&colon;icon&equals;"&commat;drawable&sol;tools"&NewLine; android&colon;title&equals;"Tools"&NewLine;&NewLine; &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;group&gt&semi;&NewLine;&NewLine; &lt&semi;item android&colon;title&equals;"Communicate"&gt&semi;&NewLine; &lt&semi;menu&gt&semi;&NewLine; &lt&semi;item&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;nav&lowbar;share"&NewLine; android&colon;icon&equals;"&commat;android&colon;drawable&sol;ic&lowbar;menu&lowbar;share"&NewLine; android&colon;title&equals;"Share" &sol;&gt&semi;&NewLine; &lt&semi;item&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;nav&lowbar;send"&NewLine; android&colon;icon&equals;"&commat;android&colon;drawable&sol;ic&lowbar;menu&lowbar;send"&NewLine; android&colon;title&equals;"Send" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;menu&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;item&gt&semi;&NewLine;&lt&semi;&sol;menu&gt&semi;<&sol;code><&sol;pre>&NewLine;<p><strong>7<&sol;strong>&period; Under <span style&equals;"color&colon; &num;008000&semi;"><strong>res &Implies; layout<&sol;strong><&sol;span>&comma; create a file named <span style&equals;"color&colon; &num;008000&semi;"><strong>nav&lowbar;header&lowbar;main&period;xml<&sol;strong><&sol;span> This layout renders the navigation drawer header part with a profile image&comma; name and email&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">nav&lowbar;header&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"176dp"&NewLine; android&colon;background&equals;"&commat;drawable&sol;background"&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentBottom&equals;"true"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"20dp"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"8dp"&NewLine; android&colon;orientation&equals;"horizontal"&gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;img&lowbar;profile"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;gravity&equals;"center"&NewLine; android&colon;src&equals;"&commat;drawable&sol;profile" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignRight&equals;"&commat;&plus;id&sol;img&lowbar;profile"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"8dp"&NewLine; android&colon;orientation&equals;"vertical"&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;centerInParent&equals;"true"&NewLine; android&colon;text&equals;"Arun Kumar"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"18sp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;centerInParent&equals;"true"&NewLine; android&colon;text&equals;"arunkumar&commat;gmail&period;com"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"15sp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;&NewLine;<&sol;code><&sol;pre>&NewLine;<p><strong>8<&sol;strong>&period; Now we’ll create another layout file to add required <strong><span style&equals;"color&colon; &num;008000&semi;">Toolbar<&sol;span> <&sol;strong>and <span style&equals;"color&colon; &num;008000&semi;"><strong>FrameLayout<&sol;strong><&sol;span>&period; FrameLayout is used to load appropriate fragment when an item is selected from nav menu&period; Create a layout file named <span style&equals;"color&colon; &num;008000&semi;"><strong>app&lowbar;bar&lowbar;main&period;xml<&sol;strong><&sol;span> under <span style&equals;"color&colon; &num;008000&semi;"><strong>res &Implies; layout<&sol;strong>&period;<&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">app&lowbar;bar&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;design&period;widget&period;AppBarLayout&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;appbar&lowbar;layout"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;theme&equals;"&commat;style&sol;MyMaterialTheme&period;AppBarOverlay"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;v7&period;widget&period;Toolbar&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;toolbar"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"&quest;attr&sol;actionBarSize"&NewLine; android&colon;background&equals;"&quest;attr&sol;colorPrimary"&NewLine; app&colon;popupTheme&equals;"&commat;style&sol;MyMaterialTheme&period;PopupOverlay" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;android&period;support&period;design&period;widget&period;AppBarLayout&gt&semi;&NewLine;&NewLine; &lt&semi;FrameLayout&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;frame&lowbar;layout"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;below&equals;"&commat;&plus;id&sol;appbar&lowbar;layout"&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;FrameLayout&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;code><&sol;pre>&NewLine;<p><strong>9<&sol;strong>&period; Open the layout file of your main activity <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span> and add <span style&equals;"color&colon; &num;008000&semi;"><strong>NavigationView<&sol;strong><&sol;span> element&period; You can notice that the toolbar layout is added using <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;include&gt&semi;<&sol;strong><&sol;span> tag&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;android&period;support&period;v4&period;widget&period;DrawerLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;drawer&lowbar;layout"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;fitsSystemWindows&equals;"true"&NewLine; tools&colon;openDrawer&equals;"start"&NewLine; tools&colon;context&equals;"com&period;c1ctech&period;navigationdrawerexample&period;MainActivity"&NewLine; &gt&semi;&NewLine;&NewLine; &lt&semi;include&NewLine; layout&equals;"&commat;layout&sol;app&lowbar;bar&lowbar;main"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;design&period;widget&period;NavigationView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;nav&lowbar;view"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;gravity&equals;"start"&NewLine; android&colon;fitsSystemWindows&equals;"true"&NewLine; app&colon;headerLayout&equals;"&commat;layout&sol;nav&lowbar;header&lowbar;main"&NewLine; app&colon;menu&equals;"&commat;menu&sol;activity&lowbar;main&lowbar;drawer" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;android&period;support&period;v4&period;widget&period;DrawerLayout&gt&semi;<&sol;code><&sol;pre>&NewLine;<p><strong>10<&sol;strong>&period; Now we have all the required elements in place&period; It’s time to open the main activity and do the necessary changes to make the navigation drawer functional&period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and modify as explained below&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;navigationdrawerexample&semi;&NewLine;&NewLine;import android&period;content&period;Intent&semi;&NewLine;import android&period;support&period;design&period;widget&period;NavigationView&semi;&NewLine;import android&period;support&period;v4&period;view&period;GravityCompat&semi;&NewLine;import android&period;support&period;v4&period;widget&period;DrawerLayout&semi;&NewLine;import android&period;support&period;v7&period;app&period;ActionBarDrawerToggle&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;&NewLine;import android&period;support&period;v7&period;widget&period;Toolbar&semi;&NewLine;import android&period;view&period;MenuItem&semi;&NewLine;&NewLine;import com&period;c1ctech&period;navigationdrawerexample&period;fragment&period;ToolsFragment&semi;&NewLine;import com&period;c1ctech&period;navigationdrawerexample&period;fragment&period;GalleryFragment&semi;&NewLine;import com&period;c1ctech&period;navigationdrawerexample&period;fragment&period;HomeFragment&semi;&NewLine;import com&period;c1ctech&period;navigationdrawerexample&period;fragment&period;ImportFragment&semi;&NewLine;import com&period;c1ctech&period;navigationdrawerexample&period;fragment&period;SlideshowFragment&semi;&NewLine;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity implements NavigationView&period;OnNavigationItemSelectedListener &lbrace;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; Toolbar toolbar &equals; &lpar;Toolbar&rpar; findViewById&lpar;R&period;id&period;toolbar&rpar;&semi;&NewLine; setSupportActionBar&lpar;toolbar&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;setting home as default fragment<&sol;span>&NewLine; getSupportActionBar&lpar;&rpar;&period;setTitle&lpar;"Home"&rpar;&semi;&NewLine; HomeFragment home&lowbar;fragment &equals; new HomeFragment&lpar;&rpar;&semi;&NewLine; getSupportFragmentManager&lpar;&rpar;&period;beginTransaction&lpar;&rpar;&period;replace&lpar;R&period;id&period;frame&lowbar;layout&comma; home&lowbar;fragment&rpar;&period;commit&lpar;&rpar;&semi;&NewLine;&NewLine; DrawerLayout drawer &equals; &lpar;DrawerLayout&rpar; findViewById&lpar;R&period;id&period;drawer&lowbar;layout&rpar;&semi;&NewLine;&NewLine; ActionBarDrawerToggle toggle &equals; new ActionBarDrawerToggle&lpar;&NewLine; this&comma; drawer&comma; toolbar&comma; R&period;string&period;navigation&lowbar;drawer&lowbar;open&comma; R&period;string&period;navigation&lowbar;drawer&lowbar;close&rpar;&semi;&NewLine; drawer&period;addDrawerListener&lpar;toggle&rpar;&semi;&NewLine; toggle&period;syncState&lpar;&rpar;&semi;&NewLine; NavigationView navigationView &equals; &lpar;NavigationView&rpar; findViewById&lpar;R&period;id&period;nav&lowbar;view&rpar;&semi;&NewLine; <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;called when any of the given navigation items selected<&sol;span>&NewLine; navigationView&period;setNavigationItemSelectedListener&lpar;this&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;onBackPress operation<&sol;span>&NewLine; &commat;Override&NewLine; public void onBackPressed&lpar;&rpar; &lbrace;&NewLine; DrawerLayout drawer &equals; &lpar;DrawerLayout&rpar; findViewById&lpar;R&period;id&period;drawer&lowbar;layout&rpar;&semi;&NewLine; &sol;&sol;if navigationDrawer is open then it will close it&NewLine; if &lpar;drawer&period;isDrawerOpen&lpar;GravityCompat&period;START&rpar;&rpar; &lbrace;&NewLine; drawer&period;closeDrawer&lpar;GravityCompat&period;START&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; super&period;onBackPressed&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public boolean onNavigationItemSelected&lpar;MenuItem item&rpar; &lbrace;&NewLine;&NewLine; int id &equals; item&period;getItemId&lpar;&rpar;&semi;&NewLine;&NewLine; if &lpar;id &equals;&equals; R&period;id&period;nav&lowbar;home&rpar; &lbrace;&NewLine; getSupportActionBar&lpar;&rpar;&period;setTitle&lpar;"Home"&rpar;&semi;&NewLine; HomeFragment home&lowbar;fragment &equals; new HomeFragment&lpar;&rpar;&semi;&NewLine; getSupportFragmentManager&lpar;&rpar;&period;beginTransaction&lpar;&rpar;&period;replace&lpar;R&period;id&period;frame&lowbar;layout&comma; home&lowbar;fragment&rpar;&period;commit&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub; else if &lpar;id &equals;&equals; R&period;id&period;nav&lowbar;import&rpar; &lbrace;&NewLine; getSupportActionBar&lpar;&rpar;&period;setTitle&lpar;"Import"&rpar;&semi;&NewLine; ImportFragment import&lowbar;fragment &equals; new ImportFragment&lpar;&rpar;&semi;&NewLine; getSupportFragmentManager&lpar;&rpar;&period;beginTransaction&lpar;&rpar;&period;replace&lpar;R&period;id&period;frame&lowbar;layout&comma; import&lowbar;fragment&rpar;&period;commit&lpar;&rpar;&semi;&NewLine; &rcub; else if &lpar;id &equals;&equals; R&period;id&period;nav&lowbar;gallery&rpar; &lbrace;&NewLine; getSupportActionBar&lpar;&rpar;&period;setTitle&lpar;"Gallery"&rpar;&semi;&NewLine; GalleryFragment gallery&lowbar;fragment &equals; new GalleryFragment&lpar;&rpar;&semi;&NewLine; getSupportFragmentManager&lpar;&rpar;&period;beginTransaction&lpar;&rpar;&period;replace&lpar;R&period;id&period;frame&lowbar;layout&comma; gallery&lowbar;fragment&rpar;&period;commit&lpar;&rpar;&semi;&NewLine; &rcub; else if &lpar;id &equals;&equals; R&period;id&period;nav&lowbar;slideshow&rpar; &lbrace;&NewLine; getSupportActionBar&lpar;&rpar;&period;setTitle&lpar;"Slideshow"&rpar;&semi;&NewLine; SlideshowFragment slideshow&lowbar;fragment &equals; new SlideshowFragment&lpar;&rpar;&semi;&NewLine; getSupportFragmentManager&lpar;&rpar;&period;beginTransaction&lpar;&rpar;&period;replace&lpar;R&period;id&period;frame&lowbar;layout&comma; slideshow&lowbar;fragment&rpar;&period;commit&lpar;&rpar;&semi;&NewLine; &rcub; else if &lpar;id &equals;&equals; R&period;id&period;nav&lowbar;tools&rpar; &lbrace;&NewLine; getSupportActionBar&lpar;&rpar;&period;setTitle&lpar;"Tools"&rpar;&semi;&NewLine; ToolsFragment tools&lowbar;fragment &equals; new ToolsFragment&lpar;&rpar;&semi;&NewLine; getSupportFragmentManager&lpar;&rpar;&period;beginTransaction&lpar;&rpar;&period;replace&lpar;R&period;id&period;frame&lowbar;layout&comma; tools&lowbar;fragment&rpar;&period;commit&lpar;&rpar;&semi;&NewLine; &rcub; else if &lpar;id &equals;&equals; R&period;id&period;nav&lowbar;share&rpar; &lbrace;&NewLine;&NewLine; Intent intent &equals; new Intent&lpar;this&comma; ShareActivity&period;class&rpar;&semi;&NewLine; startActivity&lpar;intent&rpar;&semi;&NewLine; &rcub; else if &lpar;id &equals;&equals; R&period;id&period;nav&lowbar;send&rpar; &lbrace;&NewLine; Intent intent &equals; new Intent&lpar;this&comma; SendActivity&period;class&rpar;&semi;&NewLine; startActivity&lpar;intent&rpar;&semi;&NewLine; &rcub;&NewLine; <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;when item is selected then it will close the drawer<&sol;span>&NewLine; DrawerLayout drawer &equals; &lpar;DrawerLayout&rpar; findViewById&lpar;R&period;id&period;drawer&lowbar;layout&rpar;&semi;&NewLine; drawer&period;closeDrawer&lpar;GravityCompat&period;START&rpar;&semi;&NewLine;&NewLine; return true&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><strong>When you run the app it will look like this as shown below &colon;<&sol;strong><&sol;p>&NewLine;<p><img class&equals;"wp-image-730 size-large aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;08&sol;Screenshot&lowbar;1533814462-576x1024&period;png" alt&equals;"" width&equals;"576" height&equals;"1024" &sol;>&NewLine;

Exit mobile version