How to use navigation drawer activity in Android Studio

[This post was updated on 13-03-2017 ]

We will show you the correct way of creating navigation drawer in android .

1.  We will deal with error cause by : onFragmentInteraction

2. Set a drawer element selected by default.

3. Change toolbar title based on active fragment.

NUTSHELL:-

  1. We have started to use Fragments instead of Activities. ( in short, fragments are light weight)
  2. To create Fragments in your main project  i.e Right click on the project folder -> New -> Fragments -> Fragment (Blank).
  3.  <FrameLayout> </FrameLayout> is used as a container for Fragments
  4. Code for Fragment Switching:

 

NAVIGATION DRAWER ACTIVITY

Download

INTRO :-

  1. Create a new Android Project  ->  Minimum SDK: 15  -> Navigation Drawer Activity -> Finish.

How to use navigation layout activity in android studio

2. Now let us examine the files that are automatically generated by Android studio.

Layout Folder:

  • content_main.xml : This is basically the First Screen i.e initially it contains only a TextView with Hello World!!We will replace the inner content with <frame-layout> , so that we can display our fragments.

content_main.xml

  • app_bar_main.xml: Contains the Toolbar overlay and Floating button.

app_bar_main.xml

  • nav_header_main.xml: Contains a part of navigation drawer.

nav_header_main.xml

  • activity_main.xml: Links  app_bar_main  &  nav_header_main (the menu items are added from menu/activity_main_drawer.xml )

Menu Folder:

  • menu/main.xml: Contains the menu items for App Tool Bar (By default it will contain only settings)

menu/main.xml

  • menu/activity_main_drawer.xml : Contains the menu items for navigation drawer.

Java Package Folder:

MainActivity.java : The main java file where you write the code (Basically onClickListeners  for navigation drawer menus and Tool bar menus.)

 

STEPS :-

1. Hope you have already created a navigation drawer project.

2. Create a new Fragment. Name it as Fragment 1.

3. Create 2 other fragments. Name them as Fragment2  and Fragment3.

4. Open content_main.xml and replace the inner textfield with framelayout.

5. Since we have created only three fragments, we will modify the drawer menu, so that it displays only three options.

Goto : res > menu > activity_main_drawer.xml . Replace its contents with the following

6. Now comes the MainActivity.java .

(1) In the onCreate() add the following to the end of the function.

 

(2) Replace onNavigationItemSelected()

 

(3) In the extends AppCompactActivity implements part , add the OnFragmentInteractionListener for all the 3 fragments.

 

(4) Now un/fortunately you will be seeing a whole bunch of red underlinings. Dont worry, press Alt + Enter and resolve the error by auto implementing onFragmentInteraction()

Why do we need it ? Basically, I use to set the toolbar title when i toggle between different fragments.

So let modify it accordingly.

We are done with the MainAcitivity.java

7. We will now open Fragment1.java. There is hell alot of boiler plate code in there.

Just to give a brief overview. ( Deviating from the main course)

  • public static Fragment1 newInstance() is use for creating fragment objects passing some parameters.
  • public void onCreate() is used for initializing/doing some non graphical things. For eg:- Initializing firebase database, or gps object. ( Do not use include functions that will modify the android screen here. It will bring crashes, as view object is created.)
  • onAttach() is called when fragment 1 is currently being displayed. For eg:- You can start the locationChangeListner() for map in case, you are view the map fragment.
  • onDetach() is called when fragment 1 is now removed from the displayed view. You can pause locationChangeListner() as you don’t need to update the data into map, when you are not looking at the map fragment.
  • onBackPressed() just remove it 😛
  • onCreateView() is the part where fragments view is set, ie the GUI part of fragment.  (Coming back to the main course)
  • OnFragmentInteractionListener() we will pass the needed title for fragment to MainActivity here.

8. Repeat the same for other two fragments.

Hope this helped someone !