Created
October 4, 2017 05:10
-
-
Save imhardiklakhani/7d09055f644095674cf4781871be4185 to your computer and use it in GitHub Desktop.
Change Font type of Tab in Tablayout
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="10dp" | |
android:text="DEFAULT FONT" | |
android:textSize="15dp" /> | |
<android.support.design.widget.TabLayout | |
android:id="@+id/tabOne" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="6dp" | |
android:background="@color/colorAccent" | |
app:tabSelectedTextColor="@android:color/black" | |
app:tabTextColor="@android:color/white" /> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="10dp" | |
android:text="CUSTOM FONT" | |
android:textSize="15dp" /> | |
<android.support.design.widget.TabLayout | |
android:id="@+id/tabTwo" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="6dp" | |
android:background="@color/colorAccent" | |
app:tabSelectedTextColor="@android:color/black" | |
app:tabTextColor="@android:color/white" /> | |
</LinearLayout> |
This file contains 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
//Add Support design dependency for Tablayout | |
... | |
compile 'com.android.support:design:25.3.1' | |
.. |
This file contains 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
import android.graphics.Typeface; | |
import android.os.Bundle; | |
import android.support.design.widget.TabLayout; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
private TabLayout mCustomFontTab; | |
private TabLayout mDefaultFontTab; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mDefaultFontTab = (TabLayout) findViewById(R.id.tabOne); | |
mCustomFontTab = (TabLayout) findViewById(R.id.tabTwo); | |
//Adding Tab to first Tab Layout | |
mDefaultFontTab.addTab(mDefaultFontTab.newTab().setText("ONE")); | |
mDefaultFontTab.addTab(mDefaultFontTab.newTab().setText("TWO")); | |
mDefaultFontTab.addTab(mDefaultFontTab.newTab().setText("THREE")); | |
//Adding Tab to second Tab Layout | |
mCustomFontTab.addTab(mCustomFontTab.newTab().setText("ONE")); | |
mCustomFontTab.addTab(mCustomFontTab.newTab().setText("TWO")); | |
mCustomFontTab.addTab(mCustomFontTab.newTab().setText("THREE")); | |
//call this method to change the font of second tab layout | |
setCustomFont(); | |
} | |
public void setCustomFont() { | |
ViewGroup vg = (ViewGroup) mCustomFontTab.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) { | |
//Put your font in assests folder | |
//assign name of the font here (Must be case sensitive) | |
((TextView) tabViewChild).setTypeface(Typeface.createFromAsset(getAssets(), "Nosifer-Regular.ttf")); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment