Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomNavigationView display both icons and text labels at all times

I am using android.support.design.widget.BottomNavigationView from design support library version 25

compile 'com.android.support:design:25.0.0'  <android.support.design.widget.BottomNavigationView         android:id="@+id/bottomBar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_gravity="center"         app:itemBackground="@color/colorPrimary"         app:menu="@menu/bottom_navigation_main"         android:forceHasOverlappingRendering="true"/> 

When there are only three actions in @menu/bottom_navigation_main, it displays both icons and text labels at all times.

What is the way to display both icons and text labels at all the time when there are more than three actions.

like image 684
Ammad Ali Avatar asked Nov 03 '16 08:11

Ammad Ali


2 Answers

For anyone still looking for a solution and doesn't want to rely on third party libraries or runtime reflection, BottomNavigationView in Support Library 28/Jetpack natively supports always having text label.

This is the method you're looking for.

Or in XML, app:labelVisibilityMode="labeled"

like image 55
shaishgandhi Avatar answered Sep 23 '22 23:09

shaishgandhi


UPDATE FROM May 8, 2018

You can use app:labelVisibilityMode="labeled" directly in <android.support.design.widget.BottomNavigationView />

Source: https://developer.android.com/reference/com/google/android/material/bottomnavigation/LabelVisibilityMode

Don't need this below lengthy solution.

PREVIOUS ANSWER

I had some weird behavior with BottomNavigationView. When I was selecting any item/fragment in it, the fragment pushes BottomNavigationView a bit lower, so text of BottomNavigationView goes below the screen, so only icons were visible and text goes hidden on clicking of any item.

If you are facing that weird behavior then Here is the solution. Just remove

android:fitsSystemWindows="true" 

in your root layout of fragment. Just remove this and boom! BottomNavigationView will work fine, now it can be shown with text and icon. I had this in my root CoordinatorLayout of fragment.

Also don't forget to add

BottomNavigationViewHelper.removeShiftMode(bottomNavigationView); 

in your activity to disable shifting mode.

Here is that class:

public class BottomNavigationViewHelper {      @SuppressLint("RestrictedApi")     public static void removeShiftMode(BottomNavigationView view) {         //this will remove shift mode for bottom navigation 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");         }     } } 
like image 42
Kishan Solanki Avatar answered Sep 20 '22 23:09

Kishan Solanki