Tutorial: BottomNavigationView and ViewPager in Xamarin.Android

Android introduced BottomNavigationView in API 25.1 and I like it. To be honest, one of the biggest difference between iOS and Android for me was the placement of tabs. I enjoyed ActionBar tabs when they were a thing and loved the fact that I could swipe the tabs as I wished. But, when I used the bottom tabs in Android recently, I fell in love instantly. In today’s world of giant smartphones, one can only reach the bottom of the device during one-handed operations, so I loved the fact that I could use my hardware back button to navigate back and switch between tabs all with a single thumb.

BottomNavigationView does not have a swiping action out of the box (and for good reason), it allows three items to have icon and title with a selection animation. More than three tabs turn into icon only until selected mode. Of course there is a way to customize all that to your needs.


Covered in this tutorial:

In this tutorial, I will go over setting up the BottomNavigationView itself. Then, I will marry it to ViewPager that will allow a nice swiping action between tabs.

Not covered in this tutorial:

I will not cover customizing the BottomNavigationView anymore than simply giving it a selection/accent color. This can come later.


    1. Setup BottomNavigationView
    2. Setup ViewPager
    3. Link BottomNavigationView and ViewPager

Let’s get started.

Step 0: Getting started

Let’s start by creating a solution in Visual Studio for Mac (my IDE for this tutorial).


Since I am dealing with Fragments, I will change my MainActivity base class to Android.Support.V4.App.FragmentActivity, and yes we will be using Android Support library V4.

 public class MainActivity : Android.Support.V4.App.FragmentActivity

Now, let’s create a layout for our MainActivity. We will add a FrameLayout to hold our Fragment, a ViewPager, and a BottomNavigationView from the Android.Support.Design library,


  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_above="@+id/bottom_navigation" />
         android:layout_above="@id/bottom_navigation" />
         app:menu="@menu/navigation_main" />

Let’s create a layout for Fragment as well. We will keep it simple with an icon and a label.


 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_centerInParent="true" />
         android:layout_centerVertical="false" />

Step 1: Setup BottomNavigationView

Let’s get to that bottom navigation. First, we need the navigation items (tabs) for the BottomNavigationView, we will add a navigation menu for this.

Navigation Menu:


 <menu xmlns:android="http://schemas.android.com/apk/res/android"
       app:showAsAction="always" />
       app:showAsAction="always" />
       app:showAsAction="always" />
       app:showAsAction="always" />

Notice the orderInCategory, this is the order of tab items from left to right.

Alright, so far so good. We have put together the views we will be working with. Now let’s get to that BottomNavigationView.

In MainActivity get hold of the BottomNavigationView and set it up.

// find the view 
_navigationView = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);

If you run the app now, you will see this,


what happened here. Well, the BottomNavigationView hides the labels if the tabs grow more than 3. So, let’s change that. Add this helper method to your code and call it when setting up the BottomNavigationView like this,

// under the setup
void RemoveShiftMode(BottomNavigationView view) {
    var menuView = (BottomNavigationMenuView) view.GetChildAt(0);
       var shiftingMode = menuView.Class.GetDeclaredField("mShiftingMode");
       shiftingMode.Accessible = true;
       shiftingMode.SetBoolean(menuView, false);
       shiftingMode.Accessible = false;
       for (int i = 0; i < menuView.ChildCount; i++)
           var item = (BottomNavigationItemView)menuView.GetChildAt(i);
           // set checked value, so view will be updated
   } catch (System.Exception ex) {

And now, we should be okay.


And that’s it. we are done with setting up the BottomNavigationView. We have not implemented the fragments for each tab yet, so tapping on the tabs won’t present anything. We will present the fragments inside the ViewPager.

Step 2: Setup ViewPager

Now, let’s setup the ViewPager. First, the adapter,


public class ViewPagerAdapter : FragmentPagerAdapter
    Fragment[] _fragments;
    public ViewPagerAdapter(FragmentManager fm, Fragment[] fragments) : base(fm)
        _fragments = fragments;
    public override int Count => _fragments.Length;
    public override Fragment GetItem(int position) => _fragments[position];

then, the ViewPager,

// find the view
_viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);

// set the adapter
_viewPager.Adapter = new ViewPagerAdapter(SupportFragmentManager, _fragments);

and that should be it for setting up the ViewPager.

Step 3: Link BottomNavigationView and ViewPager

When the user slides between ViewPager views, ViewPager.PageSelected event is fired. Similarly, when the user taps on a tab (navigation menu item) in the BottomNavigationView, the BottomNavigationView.NavigationItemSelected event is fired. We have to link these two events to make the BottomNavigationView and ViewPager dance in sync. Here’s how we’re going to do it.

Declare an event handler for  BottomNavigationView and ViewPager as follows,

// wireup the page selection event
_viewPager.PageSelected += ViewPager_PageSelected; 

// wire up the selection event 
_navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;

When the ViewPager page is selected, we notify the BottomNavigationView and vice versa. Like this,

 private void ViewPager_PageSelected(object sender, ViewPager.PageSelectedEventArgs e)
     var item = _navigationView.Menu.GetItem(e.Position);
     _navigationView.SelectedItemId = item.ItemId;
 void NavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
     _viewPager.SetCurrentItem(e.Item.Order, true);

And that’s it. Now the BottomNavigationView and ViewPager are displaying the selected fragment/page/tab and updating each other with a smooth transition.

One last thing, load fragments for these tabs.

 void InitializeTabs()
     _fragments = new Fragment[] {

Like and/or leave a comment below, your feedback is always welcome 🙂

Get the complete code at : https://github.com/hnabbasi/BottomNavigationViewPager


2 thoughts on “Tutorial: BottomNavigationView and ViewPager in Xamarin.Android

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.