Android Font Size of Tabs

android font size of tabs

Write these below codes in styles.xml

<style name="MyTabLayout" parent="Base.Widget.Design.TabLayout">
<item name="tabTextAppearance">@style/MyTabTextAppearance</item>
</style>

<style name="MyTabTextAppearance" parent="TextAppearance.AppCompat.Button">
<item name="android:textSize">18sp</item>
<item name="android:textColor">@android:color/white</item>
<item name="textAllCaps">true</item>
</style>

And in your tablayout, set the style like below.

<android.support.design.widget.TabLayout
style="@style/MyTabLayout"
android:layout_width="width"
android:layout_height="height"/>

Change tab font size when selected/unselected in TabLayout Android

To change the Tab font size based the selected/unselected state you have to use your custom tab view and use TabLayout.OnTabSelectedListener to change the size of selected/unselected tab.

1.TabLayout in xml can be like below:

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
app:tabGravity="fill"
app:tabIndicatorColor="@android:color/holo_orange_light">

<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 1" />

<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 2" />

<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 3" />

</com.google.android.material.tabs.TabLayout>

2.Initialise for each tab a CustomView using setCustomView method and use TabLayout.OnTabSelectedListener to listen which tab is currently selected and which one is now unselected and change the text size like below:

//get each tab from tabLayout
TabLayout.Tab tab0 = tabLayout.getTabAt(0);
TabLayout.Tab tab1 = tabLayout.getTabAt(1);
TabLayout.Tab tab2 = tabLayout.getTabAt(2);

//and set for each one a custom View
tab0.setCustomView(createCustomTabView("Tab 0", 30, android.R.color.holo_green_light)); //initially this tab is selected
tab1.setCustomView(createCustomTabView("Tab 1", 15, android.R.color.black));
tab2.setCustomView(createCustomTabView("Tab 2", 15, android.R.color.black));

//add OnTabSelectedListener
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab) {
setTabTextSize(tab, 30, android.R.color.holo_green_light);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
setTabTextSize(tab, 15, android.R.color.black);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

with the below helper functions to create the Tab CustomView and to change the text size based the selected/unselected state:

private View createCustomTabView(String tabText, int tabSizeSp, int textColor){

View tabCustomView = getLayoutInflater().inflate(R.layout.tab_customview, null);
TextView tabTextView = tabCustomView.findViewById(R.id.tabTV);
tabTextView.setText(tabText);
tabTextView.setTextSize(tabSizeSp);
tabTextView.setTextColor(ContextCompat.getColor(tabCustomView.getContext(), textColor));
return tabCustomView;
}

private void setTabTextSize(TabLayout.Tab tab, int tabSizeSp, int textColor){

View tabCustomView = tab.getCustomView();
TextView tabTextView = tabCustomView.findViewById(R.id.tabTV);
tabTextView.setTextSize(tabSizeSp);
tabTextView.setTextColor(ContextCompat.getColor(tabCustomView.getContext(), textColor));
}

3.And the custom Tab layout R.layout.tab_customview can be like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">

<TextView
android:id="@+id/tabTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:textSize="15sp"
android:textAlignment="center"
android:textColor="@android:color/black"
android:maxLines="1"
tools:text="Tab"/>

</RelativeLayout>

Result:

tablayout_textsize

Changing text size inside Tabs

Well i dnt know the reason why you choose manutd.football.app.SlidingTabLayout

you can use

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
style="@style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>

by this you can change what you want in your tabs like

<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabMaxWidth">@dimen/tab_max_width</item>
<item name="tabIndicatorColor">?attr/colorAccent</item>
<item name="tabIndicatorHeight">2dp</item>
<item name="tabPaddingStart">12dp</item>
<item name="tabPaddingEnd">12dp</item>
<item name="tabBackground">?attr/selectableItemBackground</item>
<item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
<item name="tabSelectedTextColor">?android:textColorPrimary</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">14sp</item>
<item name="android:textColor">?android:textColorSecondary</item>
<item name="textAllCaps">true</item>
</style>

How to change font size in Material Design TabLayout android?

Well, I was able to find a solution to my issue.

Adding the below line of code in dimens.xml somehow made it work for me.

<dimen name="design_tab_text_size_2line" tools:override="true">20sp</dimen>

Cannot set TabItem text and icon size is always the same size

You can give a style tag to default tab layout by providing its properties like textSize, fontFamily etc.

<style name="TabTheme">
<item name="android:textSize">16sp</item>
<item name="android:textStyle">normal</item>
<item name="android:textAllCaps">true</item>
<item name="android:fontFamily">@font/normal</item>
<item name="android:textColor">@color/colorDefault</item>
<item name="android:includeFontPadding">false</item>

</style>

and use in your activity/fragment.xml as

 <android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:tabBackground="@android:color/transparent"
android:layout_gravity="center_horizontal"
app:tabMode="scrollable"
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="@color/colorBlack"
app:tabTextAppearance="@style/TabTheme" />

OUTPUT

Output of Android Tab Layout

How to change font size in TabLayout?

Go to

1> Your Library Module

2> open a file "tablayoutplus_custom_view.xml"

3> set android:textSize="20sp" into "android:id="@+id/tvTabText""

OR

Set Programatically in Library module in "BadgedTabCustomView.java":

  tvTabText.setTextSize(20);

Hope this helps you now.



Related Topics



Leave a reply



Submit