Search code examples
androidkotlintabsfont-sizebottomnavigationview

Stops BottomNavigationView from increasing items font size on click


The default behavior of BottomNavigationView makes the items fonts size increase when clicking on them, causing some longer titles to get truncated.

I'd like to avoid the font size increase, how can I do that?

Thanks!


Solution

  • add your text size in dimensions file with the following key

     <dimen name="design_bottom_navigation_active_text_size" tools:override="true">12sp</dimen>
    <dimen name="design_bottom_navigation_text_size" tools:override="true">12sp</dimen>
    

    For remove blinking, you have to disable shifting.

    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 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.setShifting(false);
                    // set once again checked value, so view will be updated
                    //noinspection RestrictedApi
                    item.setChecked(item.getItemData().isChecked());
                }
            } catch (NoSuchFieldException e) {
                Log.e("Error BottomBar", e.getLocalizedMessage());
            } catch (IllegalAccessException e) {
                Log.e("Error BottomBar", e.getLocalizedMessage());
    
            }
        }
    }
    

    Apply this class to your navigation view

    BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);