Change font of text in View Pager Tabs - Genics Blog

Full width home advertisement

Funny Tricks

Technology on the go!

Post Page Advertisement [Top]

Tabs
Today, I am going to explain to you how can you change the text of tabs in a tabbed activity of android. This is a very simple yet effective way which you won't get that easily on the Internet. So without taking any more time, let's dive into the codes.

First, you have to create assets directory(please remember to keep that out of the res folder) and put your font's TTF file there.

Then, create a method called setCustomFont() in the tabbed activity. Code it like this:
public void setCustomFont() {

        ViewGroup vg = (ViewGroup) tabs.getChildAt(0);
        int tabsCount = vg.getChildCount();

        for (int j = 0; j < tabsCount; j++) {
            ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);

            int tabChildsCount = vgTab.getChildCount();

            for (int i = 0; i < tabChildsCount; i++) {
                View tabViewChild = vgTab.getChildAt(i);
                if (tabViewChild instanceof TextView) {
                    ((TextView) tabViewChild).setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));
                }
            }
        }
    }
Custom font

Here, font.ttf is the TTF file of the font you want to use and the ViewGroup variable vg is the TabLayout in the XML with id: tabs

And then call the setCustomFont() function in the activity in the onCreate() method.
With this, you will get your desired font face of the text in the tabs.

No comments:

Post a Comment

Please be polite to others while commenting. Spam content may be removed by the authors. Please be authentic in your reviews and opinion.

Bottom Ad [Post Page]