Tab View Inside Navigation Drawer for android

To create a project that displays Tab View inside navigation drawer selection. By simple step by step process. It’s simple to combine tab view inside navigation drawer. First start by creating a “Navigation Drawer Activity” in android studio (assuming you are aware of those things). Second part is to create a fragments to handle the navigation drawer activities.

This flow chart may help you to better understand what we want to achieve.

Tab View inside Navigation Drawer image logicchip 1

FragmentA contains the Tablayout (Tab View) and view pager, the navigation drawer pass id to FragmentA it displays tabs and content to be displayed in the viewpager according to the navigation drawer and tab selected. In this case the id passed is the name of navigation drawer menu.

Tab View inside Navigation Drawer image logicchip4

This project contains.

MainActivity.java => Navigation Drawer Activity.

FragmentA.java => Fragment containing Tab View.

FragmentB.java => Fragment for viewpager.

FragmentC.java => Fragment for running first and menu without tabs.

Constants.java => Class file for keeping constant values.

SectionsPageAdapter.java => It’s a Fragment Page Adapter to handle FragmentB from FragmentA.

OnFragmentInteractionListener.java => for handling communication between Fragment and parent activity.

Below is a simple step by step explanation.


1.Constants.java

 

This class contain only key values for passing data to fragments.

 

2.OnFragmentInteractionListener.java

 

A simple interface for communicating between fragments and parent activity.

 

3. FragmentC.java

 

This fragment is only for displaying message of those menu that don’t have any tabs. The arguments are the name of navigation menu name (id).

fragment_c.xml



 

4. FragmentB.java

 

FragmentB is for displaying the contents of the tab selected. The arguments are the name of the selected tabs (id).

fragment_b.xml

 

5. SectionsPageAdapter.java

 

This class file is an adapter for handling instance of tabs selected, which displays contents of FragmentB according to the tab selected. It extents FragmentPagerAdapter class.

 

6. FragmentA.java

 

This fragment contains tablayout and viewpager. FragmentB is displayed inside viewpager. FragmentA is placed inside the parent activity, so when clicking a menu item it displays Parent Activity and inside it FragmentA with Tabs and Viewpager also FragmentB inside Viewpager.
This figure shows how it’s implemented.

Tab View Inside Navigation Drawer logicchip 2

 

fragment_a.xml

 

7. MainActivity.java

The mainactivity.java is the launcher activity (parent activity) it implements navigation item selected listener and OnFragmentInteractionListener (our 2nd step mentioned above). Coding take place in the “onNavigationItemSelected (MenuItem item)” method, handling the menu items clicks. Another method name “homeFragment ()” is also created to display when starting up otherwise we get a blank screen when starting. Back pressed method “onBackPressed ()” is slightly modified to handle back click for selected drawer menu item. All the other things we need are already included in the template “Navigation Drawer Activity in Android Studio”.
Because it’s Navigation Drawer it contains more than one xml file.

app_bar_main.xml

Download full source code for free

10 thoughts on “Tab View Inside Navigation Drawer for android

Leave a Reply

%d bloggers like this: