18

I am using the Bottom Navigation Bar in Android. By default when I select an item the text size of the label of that item increases. As seen here by the 'tournaments' label.

enter image description here

enter image description here

Is there a way to remove this so the word 'tournaments' stays the same size?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Lewis Black
  • 957
  • 2
  • 8
  • 22

6 Answers6

28

You can set the active and inactive textAppearance for a BottomNavigationView via styles:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/BottomNavigationView"/>

Put below styles in styles.xml file

<style name="BottomNavigationView">
    <item name="itemTextAppearanceActive">@style/TextAppearance.BottomNavigationView.Active</item>
    <item name="itemTextAppearanceInactive">@style/TextAppearance.BottomNavigationView.Inactive</item>
</style>

 <!-- blank styles for better code readability-->
<style name="TextAppearance"/>
<style name="TextAppearance.BottomNavigationView"/>

<!-- inactive tab icon style -->
<style name="TextAppearance.BottomNavigationView.Inactive">
    <item name="android:textSize">12sp</item>
</style>

<!-- active tab icon style -->
<style name="TextAppearance.BottomNavigationView.Active">
    <item name="android:textSize">12sp</item>
</style>

With TextAppearance you can control more than just textSize, but also properties such as fontFamily, etc.

Abhishek Garg
  • 3,092
  • 26
  • 30
Jason Grife
  • 1,197
  • 11
  • 14
  • Works for me. Thanks a lot – nuamehas Nov 28 '18 at 17:50
  • 3
    Reference to used attrs: https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/bottomnavigation/res/values/attrs.xml – nuamehas Nov 28 '18 at 18:04
27

Try to add this code in dimens.xml file

<dimen name="design_bottom_navigation_text_size" tools:override="true">10sp</dimen>
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">10sp</dimen>
Mitesh Vanaliya
  • 2,491
  • 24
  • 39
  • I don't get any error log. I even tried to change the value of text size in the library's value.xml file and set `badge_text_size` to 50sp. But still no luck. – viper Aug 02 '18 at 11:23
  • Thank you for the solution, it saved much of my time! – hetsgandhi Mar 30 '20 at 05:48
5

You have to do 2 simple things if your using support library '28.0.0-alpha1' or higher -

Add below two lines in your dimen.xml file

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

And in view -

<android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:foreground="?attr/selectableItemBackground"
            app:itemIconTint="@color/colorAccent"
            app:itemTextColor="@color/colorAccent"
            android:elevation="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:labelVisibilityMode="labeled"
            app:menu="@menu/navigation" />

Put app:labelVisibilityMode="labeled"

That's all enjoy :-)

Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63
2

All control UI BottomNavigationView, below. I used dependence com.google.android.material:material for BottomNavigationView.

 private void editBottomNavigationViewItems(BottomNavigationView bottomNavigationView) {

        for (int i = 0; i < bottomNavigationView.getChildCount(); i++) {

            try {

                View item = bottomNavigationView.getChildAt( i );

                if (item instanceof BottomNavigationMenuView) {

                    BottomNavigationMenuView menu = (BottomNavigationMenuView) item;
                    for (int j = 0; j < menu.getChildCount(); j++) {

                        try {

                            View menuItem = menu.getChildAt( j );

                            // not chosen item menu  GO
                            View _small = menuItem.findViewById(com.google.android.material.R.id.smallLabel);//dependence com.google.android.material:material
                            //View _small = menuItem.findViewById(android.support.design.R.id.smallLabel);// dependence android.support.design
                            if ( _small instanceof TextView ) {
                                //_small.setPadding(0, 0, 0, 0);// remove all padding
                                TextView _tv = (TextView)_small;
                                _tv.setTextSize( 12 );// set size text
                            }// not chosen item menu  END

                            //this chosen item menu GO
                            View _large = menuItem.findViewById(com.google.android.material.R.id.largeLabel);//dependence com.google.android.material:material
                            //View _large = menuItem.findViewById(android.support.design.R.id.largeLabel);//dependence android.support.design.R.id.largeLabel
                            if ( _large instanceof TextView ) {
                                _large.setPadding(0,0,0,0);// remove all padding
                                TextView _tv = (TextView)_large;
                                _tv.setTextSize( 12 );// set size text
                            }// this chosen item menu  END

                        } catch ( NullPointerException npei ) {
                            Log.e("TAG", "get:BottomNavigationMenuView: " + npei.getMessage() );
                        }

                    }

                }

            } catch ( NullPointerException npe ) {
                Log.e("TAG", "get:BottomNavigationView: " + npe.getMessage() );
            }

        }

    }
amiron
  • 721
  • 9
  • 11
0

Use this method in onCreate() and before bottom_navigation_menu.setOnNavigationItemSelectedListener:-

public void removePaddingFromBottomNavigationItem() {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottom_navigation_menu.getChildAt(0);
        for (int i = 0; i < menuView.getChildCount(); i++) {
            BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
            View activeLabel = item.findViewById(R.id.largeLabel);
            if (activeLabel instanceof TextView) {
                activeLabel.setPadding(0, 0, 0, 0);
            }
        }
    }

And use this code in styles.xml:-

<style name="TextAppearance.BottomNavigationView.Inactive">
    <item name="android:textSize">@dimen/_10ssp</item>
</style>

<style name="TextAppearance.BottomNavigationView.Active">
    <item name="android:textSize">@dimen/_10ssp</item>
</style>
Ankit Lathiya
  • 199
  • 1
  • 12
0

Try with below line in dimen.xml

<dimen name="design_bottom_navigation_text_size" tools:override="true">8sp</dimen>
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">8sp</dimen>
<dimen name="design_bottom_navigation_icon_size">15dp</dimen>
<dimen name="design_bottom_navigation_height">45dp</dimen>
Mahmoud D. Alghraibeh
  • 1,507
  • 2
  • 10
  • 10