Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 889
vinay vyas Avatar asked Aug 14 '15 13:08

vinay vyas


People also ask

How to set TabLayout title in android?

Tabs are created using the newTab() method of TabLayout class. The title and icon of Tabs are set through setText(int) and setIcon(int) methods of TabListener interface respectively. Tabs of layout are attached over TabLayout using the method addTab(Tab) method.

What is TabLayout?

com.google.android.material.tabs.TabLayout. TabLayout provides a horizontal layout to display tabs.


1 Answers

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

like image 183
AlexEizenhart Avatar answered Sep 20 '22 21:09

AlexEizenhart