Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long text cutting off in BottomNavigationView #139

Closed
blu3-b1rd opened this issue Jun 8, 2018 · 35 comments
Closed

Long text cutting off in BottomNavigationView #139

blu3-b1rd opened this issue Jun 8, 2018 · 35 comments

Comments

@blu3-b1rd
Copy link

Overview

Long texts cut off in BottomNavigationView only when tab is selected. When tab is unselected, it displays the text properly

When tab is unselected (Meine tickets)

screen shot 2018-06-08 at 08 45 31

When tab is selected (Meine tickets)

screen shot 2018-06-08 at 08 45 48

Reproduction steps

  • Setup bottom navigation view with 5 menu items with titles as: "Starseite", "Suchen", "Meine Tickets", "Verkaufen", "Profil"
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:id="@+id/action_home"
    android:icon="@drawable/tab_home_icon_selector"
    android:title="@string/nav_tab_home"/>

  <item
    android:id="@+id/action_explore"
    android:icon="@drawable/tab_explore_icon_selector"
    android:title="@string/nav_tab_explore"/>

  <item
    android:id="@+id/action_my_tickets"
    android:icon="@drawable/tab_tickets_icon_selector"
    android:title="@string/nav_tab_my_tickets"/>

  <item
    android:id="@+id/action_sell"
    android:icon="@drawable/tab_sell_icon_selector"
    android:title="@string/nav_tab_sell"/>

  <item
    android:id="@+id/action_profile"
    android:icon="@drawable/tab_profile_icon_selector"
    android:title="@string/nav_tab_profile"/>
</menu>
  • View style is:
<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/home.navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:labelVisibilityMode="labeled"
    app:menu="@menu/home"/>

Version number

  • Found in version 1.0.0-alpha3

Operating system and device

  • Found in Nexus 5X emulator running Android Oreo

screen shot 2018-06-08 at 08 55 37

@ppthakur
Copy link

ppthakur commented Aug 3, 2018

Please add this property in your Xml
app:labelVisibilityMode="selected"

@gojoel
Copy link

gojoel commented Aug 8, 2018

Not sure why this was closed.
I'm still having the issue with long labels being cut off when the labelVisibilityMode ="labeled".
Selected will only show label for the selected item, and I'm looking to show for all.

@kistitan
Copy link

I am also facing this issue.

labelVisibilityMode ="labeled" should work too especially if active and inactive text size are the same.
It feels a bit strange that text get truncated just because the text color is changed.

Would you mind reconsidering reopening this issue?

@t7costa
Copy link

t7costa commented Aug 24, 2018

Agreed. I'm not sure why this extra padding is needed in "labeled" mode https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/bottomnavigation/res/layout/design_bottom_navigation_item.xml#L45

@viktorsator
Copy link

Same goes for me. It would be nice to have this fixed... As @t7costa said i'm not sure why did they put an extra padding on the largeLabel...

@kistitan
Copy link

kistitan commented Sep 4, 2018

As for now I use this for the active childView:

        BottomNavigationItemView itemView = (BottomNavigationItemView) childView;
        View activeLabel = itemView.findViewById(com.google.android.material.R.id.largeLabel);
        if (activeLabel != null && activeLabel instanceof TextView) {
            ((TextView)activeLabel).setPadding(0,0,0,0);
        }

But it is not the ideal, would be nice if we had an official API to influence this.
I do not see anything on the material design website about padding for active item, so it looks a deviation for me.

@krobitai
Copy link

krobitai commented Sep 6, 2018

Would like this reopened as well. Facing the same issue and it doesn't seem to have a reason that it needs to work this way.

@kistitan How did you get the reference to the childView for the above solution?

@blu3-b1rd blu3-b1rd reopened this Sep 12, 2018
@ldjcmu
Copy link
Contributor

ldjcmu commented Sep 14, 2018

This issue is being tracked at https://issuetracker.google.com/issues/115754572

@kaustavjaiswal
Copy link

kaustavjaiswal commented Oct 8, 2018

Till Google Fixes this, I was able to to use @kistitan 's solution and also add this to the mix to help truncate the text properly in Kotlin:
activeLabel.ellipsize = TextUtils.TruncateAt.END

@DhanalakshmiS
Copy link

DhanalakshmiS commented Oct 16, 2018

Also, we can override these dimens to change the text sizes of the labels in the active and inactive state.

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

Simply add these in your dimens.xml, so that the default text size will be overridden.

@mapo-lp
Copy link

mapo-lp commented Oct 16, 2018

@kistitan and @kaustavjaiswal's solutions works like a chaaarm, thanks!

@fatihkizmaz
Copy link

fatihkizmaz commented Oct 25, 2018

This problem still persists.
As @t7costa wrote: Problem's cause is active item textview's left and right padding.
@kistitan's solution works perfectly.
So another solution to this problem is overriding design_bottom_navigation_item.xml

Add design_bottom_navigation_item.xml from https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/bottomnavigation/res/layout/design_bottom_navigation_item.xml to your project's layout directory with name bottom_navigation_item.xml and delete left and right padding.
And then add this line to your project's any values resource file:
<item name="design_bottom_navigation_item" type="layout" tools:override="true">@layout/bottom_navigation_item</item>

@afohrman
Copy link
Contributor

This is being tracked in the Issue Tracker: https://issuetracker.google.com/115754572

@sagrawal2418
Copy link

sagrawal2418 commented Oct 26, 2018

public void removePaddingFromNavigationItem() {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.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);
        }
    }
}

I used the global navigation view variable and then just call this method in the oncreate before the setOnNavigationItemSelectedListener

@nisd93
Copy link

nisd93 commented Nov 2, 2018

@sagrawal2418 works like a charm

@dimasdark
Copy link

dimasdark commented Nov 5, 2018

Use @sagrawal2418 answer:

BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.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);
        }
    }
}

in combination with @DhanalakshmiS answer, it works perfectly. There's no padding at all and text remains the same:

<!-- Bottom Menu Override -->
<dimen name="design_bottom_navigation_text_size" tools:override="true">12sp</dimen>
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">12sp</dimen>

@Ghedeon
Copy link

Ghedeon commented Nov 6, 2018

Thanks, @fk1453, seems like the cleanest solution so far.

@cdmunoz
Copy link

cdmunoz commented Dec 21, 2018

If you are using a theme within the BottomNavigationBar to set font family and text size, then overriding design_bottom_navigation_text_size and design_bottom_navigation_active_text_size won't work.
just to be clear ;)

@i-m-aman
Copy link

Thanks for the Workaround. For those coming to here for the solution, it is working perfectly.

@palmas
Copy link

palmas commented Mar 28, 2019

Thanks, @fatihkizmaz , works like a charm!=)

@mehdiyari
Copy link

app:labelVisibilityMode="unlabeled"
this line of code on 'com.google.android.material.bottomnavigation.BottomNavigationView' work for me

@ademar111190
Copy link

@kistitan and @sagrawal2418 workaround adapted for kotlin:

val menuView = bottom_navigation_view.getChildAt(0) as? ViewGroup ?: return
menuView.forEachChild {
  it.findViewById<View>(R.id.largeLabel)?.setPadding(0, 0, 0, 0)
}

@igordmv
Copy link

igordmv commented Sep 12, 2019

This is my approach: Just create a class that extends BottomNavigationView like this:

class PaddingLessBottomNavigation(context: Context, attrs: AttributeSet) :
BottomNavigationView(context, attrs) {

override fun onFinishInflate() {
    super.onFinishInflate()
    val menuView = getChildAt(0) as? ViewGroup
    menuView?.let {
        for (child in 0 until it.childCount) {
            val menuItem = it.getChildAt(child)
            val largeLabel =
                menuItem?.findViewById<View>(com.google.android.material.R.id.largeLabel)
            largeLabel?.setPadding(0, 0, 0, 0)
        }
    }
}

}

After that you just have to use your class on you layout like you do on normal BottomNavigationView:

<com.your.package.PaddingLessBottomNavigation
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="@color/bottom_navigation_background"
android:theme="@style/BottomNavigation"
app:elevation="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:labelVisibilityMode="labeled"
app:menu="@menu/menu_bottom_navigation"
/>

@saharshpruthi
Copy link

Upgrading to com.google.android.material:material:1.1.0-beta01 will fix this issue.

Steinhagen added a commit to Steinhagen/openfoodfacts-androidapp that referenced this issue Nov 3, 2019
VaiTon pushed a commit to openfoodfacts/openfoodfacts-androidapp that referenced this issue Dec 27, 2019
* WIP: Fixed menu highlights for the main bottom navigation

The current fix uses setSelectedItemId to make sure that the correct
menu item is selected. This API was introduced in SDK 25.3.0.

* Changed the bottom navigation color scheme to see clearly what item is selected

* Removed redundant bottom navigation declaration

* Fixed active/inactive size + deselected the fist item for activities not mentioned in the bottom navigation bar

* Make sure that the Home navigation item is highlighted on activity resume

* Made the Comparison activity to redirect to Home like all activities

A problem with this was that onOptionItemSelected returned true by
default which blocked the call from completing. In our case, we don't
have to override the call.

Also, the manifest needed to be modified to redirect you to the home
activity.

* Reworked bottom navigation logic + added compatibility code

* Fixed trucated navigation item when the text was to large

Fixed the issue described into this material fix:
material-components/material-components-android#139
@rafabaptista
Copy link

@layout/bottom_navigation_item

Perfect!

@rahul-rahul1
Copy link

bottomNavigation Icon by default is showing black-color.How resoved the icon issue in bottomNavigationView android?

@paulshandomain
Copy link

Upgrading to com.google.android.material:material:1.1.0 will fix long text cutting off bug.

@Mr-Ramzan
Copy link

In this theme => Theme.MaterialComponents.Light.NoActionBar.

 <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>

does not worked for me.
What I did to resolve is

<style name="BottomNavigationView.InActive" parent="@style/TextAppearance.AppCompat.Caption">
    <item name="android:textSize">8sp</item>
</style>

<style name="BottomNavigationView.Active" parent="@style/TextAppearance.AppCompat.Caption">
    <item name="android:textSize">8sp</item>
</style>

Define these in styles and used in the XML like below.

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    ...........
    app:itemTextAppearanceActive="@style/BottomNavigationView.Active"
    app:itemTextAppearanceInactive="@style/BottomNavigationView.InActive"
    .......
    android:theme="@style/BottomNavigationTheme"
    />

Hope its helpful to someone.

@manishakamble
Copy link

manishakamble commented May 22, 2020

@paulshandomain @dimas Mendes its work. thank you

@jinpang
Copy link

jinpang commented May 29, 2020

Reset BottomNavigationItemView long click listener, can resolve long text cutting off in BottomNavigationView. e.g:
BottomNavigationItemView[] views = dataBinding.bottomNav.getBottomNavigationItemViews();
for (BottomNavigationItemView v : views) {
if (v != null) {
v.setOnLongClickListener(v1 -> true);
}
}

@arjit-meesho
Copy link

This problem still persists.
As @t7costa wrote: Problem's cause is active item textview's left and right padding.
@kistitan's solution works perfectly.
So another solution to this problem is overriding design_bottom_navigation_item.xml

Add design_bottom_navigation_item.xml from https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/bottomnavigation/res/layout/design_bottom_navigation_item.xml to your project's layout directory with name bottom_navigation_item.xml and delete left and right padding.
And then add this line to your project's any values resource file:
<item name="design_bottom_navigation_item" type="layout" tools:override="true">@layout/bottom_navigation_item</item>

I used this solution with AutoSizing. This works absolutely fine for me.

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginTop="@dimen/_8dp"
        android:layout_marginBottom="@dimen/_8dp"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@null"
        android:duplicateParentState="true"/>
    <com.google.android.material.internal.BaselineLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:paddingBottom="10dp"
        android:clipToPadding="false"
        android:duplicateParentState="true">
        <TextView
            android:id="@+id/smallLabel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dimen_12_dp"
            android:layout_marginRight="@dimen/dimen_12_dp"
            android:duplicateParentState="true"
            app:autoSizeTextType="uniform"
            app:autoSizeMinTextSize="8sp"
            app:autoSizeMaxTextSize="12sp"
            app:autoSizeStepGranularity="1sp"
            android:ellipsize="end"
            android:maxLines="1"/>
        <TextView
            android:id="@+id/largeLabel"
            android:layout_width="match_parent"
            android:layout_marginLeft="@dimen/_8dp"
            android:layout_marginRight="@dimen/_8dp"
            android:layout_height="wrap_content"
            android:paddingLeft="0dp"
            android:paddingRight="0dp"
            android:duplicateParentState="true"
            android:maxLines="1"
            app:autoSizeTextType="uniform"
            app:autoSizeMinTextSize="10sp"
            app:autoSizeMaxTextSize="16sp"
            app:autoSizeStepGranularity="1sp"
            android:ellipsize="end"
            android:visibility="invisible"/>
    </com.google.android.material.internal.BaselineLayout>
</merge>

@MichaelTheBestProgrammerInTheWorld

It's fixed now all what you need is to upgrade the version as the following
implementation 'com.google.android.material:material:1.1.0'

@georgious1
Copy link

Issue still reproducable in implementation 'com.google.android.material:material:1.2.1'

@outofsleepexception
Copy link

Having the same issue with '1.3.0-alpha03'.

@abhishekgargx
Copy link

It will remove default padding, center align text
and add support for two lines, so in case of long text it will appear in two lines.

this will work with old as well new material library.

private void updateBottomViewProperties() {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
        if (menuView != null) {
            for (int i = 0; i < menuView.getChildCount(); ++i) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                if (item != null) {
                    setBottomNavTextViewProps(item.findViewById(R.id.smallLabel));
                    setBottomNavTextViewProps(item.findViewById(R.id.largeLabel));
                }
            }
        }
    }

    private void setBottomNavTextViewProps(View view){
        if (view instanceof TextView) {
            view.setPadding(0, 0, 0, 0);
            view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            ((TextView) view).setGravity(Gravity.CENTER);
            ((TextView) view).setMaxLines(2);
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests