BottomNavigationView is a component added in Google Support Library 25. It provides a quick navigation between top level views within an app. It should be used when application has three to five top-level destinations. My implementation includes the switching between Fragments on Selecting the Menu Items.
Add to the build.gradle of your project module
compile'com.android.support:design:25.3.1'
Create the BottomNavigationView in xml of your layout and provide a menu resource to it:
Create a file here navigation.xml in menu resource folder. This file is used for providing the MenuItems in BottomNavigationView
With everything in line this much code shows up the BottomBar on running the app. Now lets set the listener for the Click Events OnNavigationItemSelectedListener and OnNavigationItemReselectedListener on Menu Items -
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
return true;
case R.id.navigation_dashboard:
return true;
case R.id.navigation_notifications:
return true;
case R.id.navigation_settings:
return true;
}
return true;
}
};
private BottomNavigationView.OnNavigationItemReselectedListener mOnNavigationItemReselectedListener = new BottomNavigationView.OnNavigationItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Log.d(TAG, "Navigation Reselected ===");
break;
case R.id.navigation_dashboard:
Log.d(TAG, "Dashboard Reselected ===");
break;
case R.id.navigation_notifications:
Log.d(TAG, "Notification Reselected ===");
break;
case R.id.navigation_settings:
Log.d(TAG, "Settings Reselected ===");
break;
}
}
};
bottomNavigationView.setOnNavigationItemSelectedListener
(mOnNavigationItemSelectedListener);
bottomNavigationView.setOnNavigationItemReselectedListener
(mOnNavigationItemReselectedListener);
If the no of Menu Items are more than 3 then the selected Item will take more space in the BottomNavView and it looks a little weird as of now, may be intentionally Google has kept it like this.
This behavior is defined by ShiftingMode property of BottomNavigationView, which can't be disabled in a straightforward way as of now, as its api is not public. But there is a way through Reflection to do it :
BottomNavigationMenuView menuView = (BottomNavigationMenuView)
btmNavigationView.getChildAt(0);
try {
Field shiftingMode = menuView.getClass()
.getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item =
(BottomNavigationItemView) menuView.getChildAt(i);
item.setShiftingMode(false);
//To update view, set the checked value again
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
After calling above code result is :
For more information checkout my Github Project:
https://github.com/pmahsky/BottomNavigationViewDemo