0

I'm trying to disable shift mode of bottom navigation bar but getting the above error.I'm following this link How to disable BottomNavigationView shift mode? for tutorial. Here is my code:

public class BottomNavigationViewHelper {
    public static void disableShiftMode(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);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
dartKnightRises
  • 815
  • 15
  • 26
  • Why don't you use this solution https://stackoverflow.com/a/51147002/7666442 – AskNilesh Dec 23 '19 at 12:38
  • 1
    Does this answer your question? [How to disable BottomNavigationView shift mode?](https://stackoverflow.com/questions/40176244/how-to-disable-bottomnavigationview-shift-mode) – Alexander van Oostenrijk Dec 23 '19 at 12:39
  • @NileshRathod I followed the same tutorial but it is now throwing the error, I mentioned above. – dartKnightRises Dec 23 '19 at 12:43
  • @AlexandervanOostenrijk Not Exactly. – dartKnightRises Dec 23 '19 at 12:43
  • @PraveenGupta can you show your gradle file and device information that you use? – Son Truong Dec 24 '19 at 02:43
  • I'm using onePlus 7 pro. – dartKnightRises Dec 24 '19 at 05:39
  • dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' implementation 'com.google.android.material:material:1.0.0' implementation 'androidx.cardview:cardview:1.0.0' implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' }` – dartKnightRises Dec 24 '19 at 05:44
  • And i'm getting this error: "Attempt to invoke virtual method 'android.view.View com.google.android.material.bottomnavigation.BottomNavigationView.getChildAt(int)' on a null object reference" – dartKnightRises Dec 24 '19 at 05:55

0 Answers0