How to remove icon animation for bottom navigation view in android

How to remove icon animation for bottom navigation view in android
December 8, 2018 3 Comments Android Development,Development Pushpendra Kumar



You might be facing this issue if you are using more than three menu item in your bottom navigation, So let’s have look at how to solve this issue in a very simple way!

The code of your main activity xml file.

<!--?xml version="1.0" encoding="utf-8"?-->
    <android.support.constraint.Constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="pushpendra.wepsapps.activities.MainActivity">

        <Relativelayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.Bottomnavigationview
                android:id="@+id/navigation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignparentbottom="true"
                android:background="?android:attr/windowBackground"
                app:layout_scrollflags="scroll|enterAlways"
                app:menu="@menu/navigation">

            </android.support.design.widget.Bottomnavigationview>
        </Relativelayout>

    </android.support.constraint.Constraintlayout>

This is your code of activity_main.xml file. Now create a new java class with the name
BottomNavigationViewHelper



import android.annotation.SuppressLint;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.util.Log;
import java.lang.reflect.Field;
public class BottomNavigationViewHelper {
    @SuppressLint("RestrictedApi")
    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);
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BottomNav", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BottomNav", "Unable to change value of shift mode", e);
        }
    }
}

After writing this code you need to write one line in your main activity class file. Here is your main activity code will be..!!

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navigation = findViewById(R.id.navigation);
        BottomNavigationViewHelper.removeShiftMode(navigation);
    }
}



Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.
Leave Comment
  1. 1

    Hosein

    thanks , but Its not useful, setShiftingMode is not available anymore.

    Reply
  2. 1

    Shubham

    Did u find some other solution?

    Reply
    1. 1

      Pushpendra Kumar

      Not yet, But in new SDK you will find the better solution!

      Reply

Leave a reply

Your email address will not be published. Required fields are marked *