-
-
Save gordinmitya/37fe6abedfc03d2b7a4a68d4f081484c to your computer and use it in GitHub Desktop.
SImple wrapper class to support fonts in TabLayout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FontTabLayout extends TabLayout { | |
private Typeface typeface; | |
public FontTabLayout(Context context) { | |
super(context); | |
init(context, null); | |
} | |
public FontTabLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs); | |
} | |
public FontTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context, attrs); | |
} | |
private void init(Context context, AttributeSet attrs) { | |
String font; | |
if (attrs == null) { | |
font = FontNames.DEFAULT; | |
} else { | |
String attributeName = context.getResources().getResourceEntryName(R.attr.fontPath); | |
final int stringResourceId = attrs.getAttributeResourceValue(null, attributeName, -1); | |
if (stringResourceId > 0) { | |
font = context.getString(stringResourceId); | |
} else { | |
font = attrs.getAttributeValue(null, attributeName); | |
} | |
} | |
typeface = Typeface.createFromAsset(getContext().getAssets(), font); | |
} | |
@Override | |
public void addTab(@NonNull Tab tab, int position, boolean setSelected) { | |
super.addTab(tab, position, setSelected); | |
ViewGroup mainView = (ViewGroup) getChildAt(0); | |
ViewGroup tabView = (ViewGroup) mainView.getChildAt(tab.getPosition()); | |
int tabChildCount = tabView.getChildCount(); | |
for (int i = 0; i < tabChildCount; i++) { | |
View tabViewChild = tabView.getChildAt(i); | |
if (tabViewChild instanceof TextView) { | |
((TextView) tabViewChild).setTypeface(typeface, Typeface.NORMAL); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment