Custom font for android.support.design.widget.TabLayout

vinay vyas picture vinay vyas · Aug 14, 2015 · Viewed 12.4k times · Source

How can i use custom font for Tablayout class belonging to android.support.design.widget package? I'm using it to implement Quick Return view functionality.

Answer

AlexEizenhart picture AlexEizenhart · Mar 18, 2016

As of 23.2.0, setTabsFromPagerAdapter has been deprecated, however using a modified version of Andreyua's answer you can use setupWithViewPager instead.

    @Override
public void setupWithViewPager(ViewPager viewPager)
{
    super.setupWithViewPager(viewPager);

    if (mTypeface != null)
    {
        this.removeAllTabs();

        ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0);

        PagerAdapter adapter = viewPager.getAdapter();

        for (int i = 0, count = adapter.getCount(); i < count; i++)
        {
            Tab tab = this.newTab();
            this.addTab(tab.setText(adapter.getPageTitle(i)));
            AppCompatTextView view = (AppCompatTextView) ((ViewGroup) slidingTabStrip.getChildAt(i)).getChildAt(1);
            view.setTypeface(mTypeface, Typeface.NORMAL);
        }
    }
}

All credit goes to Andreyua for their original code snippet with a minor modification.

Unfortunately, I don't have enough reputation to make comments or I would have responded directly :)