-1

I got a BottomNavigationView in my android app which I set custom text size following this answer.

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

now since I implemented multiple languages in this app, I need to change the text size of BottomNavigationView programmatically. But since the way I set font size with dimens, I can't figure out how to change it by code now.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Ghasem
  • 14,455
  • 21
  • 138
  • 171

1 Answers1

0

You can use this method:

private void setBottomNavigationLabelsTextSize(BottomNavigationView bottomNavigationView, float ratio) {
    for (int i = 0; i < bottomNavigationView.getChildCount(); i++) {
        View item = bottomNavigationView.getChildAt(i);

        if (item instanceof BottomNavigationMenuView) {
            BottomNavigationMenuView menu = (BottomNavigationMenuView) item;

            for (int j = 0; j < menu.getChildCount(); j++) {
                View menuItem = menu.getChildAt(j);

                View small = menuItem.findViewById(android.support.design.R.id.smallLabel);
                if (small instanceof TextView) {
                    float size = ((TextView) small).getTextSize();
                    ((TextView) small).setTextSize(TypedValue.COMPLEX_UNIT_PX, ratio * size);
                }
                View large = menuItem.findViewById(android.support.design.R.id.largeLabel);
                if (large instanceof TextView) {
                    float size = ((TextView) large).getTextSize();
                    ((TextView) large).setTextSize(TypedValue.COMPLEX_UNIT_PX, ratio * size);
                }
            }
        }
    }
}

call it like:

setBottomNavigationLabelsTextSize(bottomNavigationView, 2.0f);

to double the size of text in the labels.

forpas
  • 160,666
  • 10
  • 38
  • 76