I want to add a font to the tablayout. The tabs are filled dynamically using Json. Please suggest me how to add font to the tab headers.
This is the tablayout
private void setupPager() {
viewPager.setAdapter(new CustomAdapter(getActivity().getSupportFragmentManager(),getContext()));
tabLayout.setupWithViewPager(viewPager);
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "EdgeCaps.ttf");
ViewGroup vg = (ViewGroup) tabLayout.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(tf);
}
}
}
}
This is the adapter. Here tabnames is a string array filled by json parsing.
private class CustomAdapter extends FragmentPagerAdapter {
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new ItemsMenuFragment();
return fragment;
}
@Override
public int getCount() {
return tabNames.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabNames.get(position);
}
}
}
Can anyone help me to change the font of the tablayout.I have tried to use this in the setupPager but its not working.
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "EdgeCaps.ttf");
ViewGroup vg = (ViewGroup) tabLayout.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(tf);
}
}
}