chrisjenx / Calligraphy

Custom fonts in Android the easy way...
Apache License 2.0
8.59k stars 1.1k forks source link

Support for BottomNavigationView #360

Open tmtrademarked opened 7 years ago

tmtrademarked commented 7 years ago

Hey, there. This may be a silly question, but I haven't found a good way to change the font on labels in a BottomNavigationView specifically. I can change the default font for my app using the Application hook, and via a theme - but I'd like to change it only for the BottomNavBar instead of globally.

Is there any reasonable way to hook into the BottomNavigationView by itself? (Apologies if this has been answered in a different form)

chrisjenx commented 7 years ago

If they are TextViews and Inflated and the AppCompat Lib provides TextAppearance Styles then you should be able to set those. I have a feeling 1-3 of those are false due to AppCompat not following standard android conventions.

tmtrademarked commented 7 years ago

Looks like AppCompat provides itemTextColor, but no text appearance attributes. They appear to be text views at the root of this, though. So maybe there's a hook in via find view. I'll take a look.

chrisjenx commented 7 years ago

If they are inflated, it makes things way easier, otherwise we have to do some hacky stuff like the Toolbar does.

On Mon, 9 Jan 2017 at 12:12 Tom Wilson notifications@github.com wrote:

Looks like AppCompat provides itemTextColor, but no text appearance attributes. They appear to be text views at the root of this, though. So maybe there's a hook in via find view. I'll take a look.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/360#issuecomment-271394356, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHRsWu4SD6TBqb-TgjOS8rhiBV-jycJks5rQpTLgaJpZM4LcDSv .

tmtrademarked commented 7 years ago

In case anyone else runs across this - I did find a way in to fix this. It's ugly and brittle, but it does work:

private void applyBottomNavFont() {
        // The BottomNavigationView widget doesn't provide a native way to set the appearance of
        // the text views. So we have to hack in to the view hierarchy here.
        for (int i = 0; i < mBottomNav.getChildCount(); i++) {
            View child = mBottomNav.getChildAt(i);
            if (child instanceof BottomNavigationMenuView) {
                BottomNavigationMenuView menu = (BottomNavigationMenuView) child;
                for (int j = 0; j < menu.getChildCount(); j++) {
                    View item = menu.getChildAt(j);
                    View smallItemText = item.findViewById(android.support.design.R.id.smallLabel);
                    if (smallItemText instanceof TextView) {
                        // Set font here
                    }
                    View largeItemText = item.findViewById(android.support.design.R.id.largeLabel);
                    if (largeItemText instanceof TextView) {
                        // Set font here
                    }
                }
            }
        }
    }
aggressivepixels commented 7 years ago

There is layout inflation involved, so you could simply use a style (I just tested it):

<style name="Widget.BottomNavigationView" parent="Widget.Design.BottomNavigationView">
    <item name="fontPath">@string/font_medium</item>
</style>

And then applying it like:

<android.support.design.widget.BottomNavigationView
    ...
    android:theme="@style/Widget.BottomNavigationView" />
ghost commented 7 years ago

you have to find label id that google used in its support library BottomNavigationView bottomNavigationView = (BottomNavigationView) fragmentActivity.findViewById(R.id.bottom_navigation); TextView textView = (TextView)bottomNavigationView.findViewById(R.id.menu_item_home).findViewById(R.id.largeLabel); textView.setTypeface(fontUtils.getTypeFace());

NameX44 commented 6 years ago

@iamjonathanhernandez solution's working !

Thanks !

xyznaveen commented 6 years ago

@tmtrademarked 's solution is working but @hernandezrjonathan 's is not working for me I don't know what is the cause.

nullerboy commented 5 years ago

private void applyBottomNavFont() { // The BottomNavigationView widget doesn't provide a native way to set the appearance of // the text views. So we have to hack in to the view hierarchy here. for (int i = 0; i < mBottomNav.getChildCount(); i++) { View child = mBottomNav.getChildAt(i); if (child instanceof BottomNavigationMenuView) { BottomNavigationMenuView menu = (BottomNavigationMenuView) child; for (int j = 0; j < menu.getChildCount(); j++) { View item = menu.getChildAt(j); View smallItemText = item.findViewById(android.support.design.R.id.smallLabel); if (smallItemText instanceof TextView) { // Set font here } View largeItemText = item.findViewById(android.support.design.R.id.largeLabel); if (largeItemText instanceof TextView) { // Set font here } } } } }

Works!