How to use navigation drawer & tabbed activity in Android Studio

New to Android Studio? Want to know how use Navigation drawer activity or Tabbed activity in your Android project. Don’t know where to start for Androids materialistic design? Then you are in the right place!!

NUTSHELL:-

  1. Androids materialistic designs uses Fragments instead of Activities.
  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!!

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.)

 

LEVEL  1 [BASICS] :-

To use navigation drawer feature, follow the steps below:

  1. Create a new Fragement and name it FragmentMain (Right click on the project folder -> New -> Fragments -> Fragment Blank ).
  2. Put some dummy elements (ie a TextView with text “Main Fragment”)
  3. FragmentMain will be set a the default main page of your App.
  4. Create another Fragment and name it FragmentCamera and put some dummy text (ie TextView with text “Camera Fragment” )
  5. open the “content_main.xml” file located in the “layout” folder. use the code below:

 

6. Go to MainActivty.java

  •  Insert these  import statements.

  • In the FragmentMain.java and FragmentCamera.java , Replace

with

 

 

7. . Insert these codes as shown in the screenshot below . [ This is used to show FragmentMain when the App starts up.]

 

 

fragment manager

 

FragmentManager Explained:

8. Now insert this for using the navigation_drawer_feature.

navigation_fragments

9. Run the app. And select Camera from the Drawer and check if it working.

 

navigation drawer working

 

TABBED ACTIVITY

The same concept of fragments applies to tabbed activity , but with some modifications.

  1. Create an Android Project with Tabbed Activity.
  2. Create 3 Fragments namely : FirstFrag, SecondFrag, ThirdFrag.
  3. In the MainActivity replace the SectionsPageAdapter as follow:

3. Click Run . Now you know the basic of Navigation and Tabbed Layout. 🙂