You will learn:
- Adding bottom
navigation drawer
- Disabling
shifting mode in bottom nav
Prerequisite:
- Should know to
create a new project in Android Studio
Follow the steps:
1.
Go to build.gradle (Module: app) and add the
following dependency and sync the project
compile 'com.android.support:design:27.1.0'
2.
Go to the layout file and add the following xml
code
<android.support.design.widget.BottomNavigationView android:id="@+id/bottom_nav_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="start" design:menu="@menu/menu_bottom_nav"/>
For @menu/menu_bottom_nav :
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_home" android:title="Home" android:icon="@drawable/ic_home_black_24dp"/> <item android:id="@+id/menu_exercise" android:title="Exercise" android:icon="@drawable/ic_exercise_black_24dp"/> <item android:id="@+id/menu_community" android:title="Community" android:icon="@drawable/ic_forum_black_24dp"/> <item android:id="@+id/menu_profile" android:title="Profile" android:icon="@drawable/ic_person_black_24dp"/> </menu>
3.
Now initialize the bottom navigation drawer in
your respective activity or fragment
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_view);
Now run the project and bottom navigation is ready. Here the
bottom navigation has shifting mode. To remove the shifting mode:
1. Create a class.
public class BottomNavigationViewHelper { public static void removeShiftMode(BottomNavigationView view) { BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.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); // set once again checked value, so view will be updated item.setChecked(item.getItemData().isChecked()); } } catch (NoSuchFieldException e) { Log.e("ERROR NO SUCH FIELD", "Unable to get shift mode field"); } catch (IllegalAccessException e) { Log.e("ERROR ILLEGAL ALG", "Unable to change value of shift mode"); } } }
2. In your activity or fragment add the following code:
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
Comments
Post a Comment