I have difficulties changing the text size of the tabs of design library tablayout (android.support.design.widget.TabLayout).
I managed to change it by assigning tabTextAppearance in TabLayout
app:tabTextAppearance="#style/MyTabLayoutTextAppearance"
the following style
<style name="MyTabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
<item name="android:textSize">14sp</item>
</style>
but I have 2 side effects :
1) I lost the accent color of the selected tab
2) The tab text is not capitalized any more.
<style name="MineCustomTabText" parent="TextAppearance.Design.Tab">
<item name="android:textSize">16sp</item>
</style>
Use is in TabLayout like this
<android.support.design.widget.TabLayout
app:tabTextAppearance="#style/MineCustomTabText"
...
/>
Go on using tabTextAppearance as you did but
1) to fix the capital letter side effect add textAllCap in your style :
<style name="MyTabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
<item name="android:textSize">14sp</item>
<item name="android:textAllCaps">true</item>
</style>
2) to fix the selected tab color side effect add in TabLayout xml the following library attributes :
app:tabSelectedTextColor="#color/color1"
app:tabTextColor="#color/color2"
Hope this helps.
Work on api 22 & 23
Make this style :
<style name="TabLayoutStyle" parent="Base.Widget.Design.TabLayout">
<item name="android:textSize">12sp</item>
<item name="android:textAllCaps">true</item>
</style>
And apply it to your tablayout :
<android.support.design.widget.TabLayout
android:id="#+id/contentTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/list_gray_border"
app:tabTextAppearance="#style/TabLayoutStyle"
app:tabSelectedTextColor="#color/colorPrimaryDark"
app:tabTextColor="#color/colorGrey"
app:tabMode="fixed"
app:tabGravity="fill"/>
Do as following.
1. Add the Style to the XML
<style name="MyTabLayoutTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">14sp</item>
</style>
2. Apply Style
Find the Layout containing the TabLayout and add the style. The added line is bold.
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabTextAppearance="#style/MyTabLayoutTextAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Try the snipped which is mentioned below, it works for me also.
In my layout xml where I have my TabLayout, have added style to the TabLayout like below :
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
and in my style.xml I have defined the style that is used in my layout xml, check code for styles added below :
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="android:background">YOUR BACKGROUND COLOR</item>
<item name="tabTextAppearance">#style/MyCustomTabText</item>
<item name="tabSelectedTextColor">SELECTED TAB TEXT COLOR</item>
<item name="tabIndicatorColor">SELECTED TAB INDICATOR COLOR</item>
</style>
<style name="MyCustomTabText" parent="TextAppearance.AppCompat.Button">
<item name="android:textSize">YOUR TEXT SIZE</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#android:color/white</item>
</style>
I hope it will work for you.....
I have similar problem and similar resolution:
1) Size
in the xml you have TabLayout,
<android.support.design.widget.TabLayout
...
app:tabTextAppearance="#style/CustomTextStyle"
...
/>
then in style,
<style name="CustomTextStyle" parent="#android:style/TextAppearance.Widget.TabWidget">
<item name="android:textSize">16sp</item>
<item name="android:textAllCaps">true</item>
</style>
If you do not want the characters in uppercase put false in "android:textAllCaps"
2) Text color of selected or unselected Tabs,
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tabLayout.setTabTextColors(getResources().getColorStateList(R.color.tab_selector,null));
} else {
tabLayout.setTabTextColors(getResources().getColorStateList(R.color.tab_selector));
}
then in res/color/tab_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white" android:state_selected="true" />
<item android:color="#color/white" />
TabLayout tab_layout = (TabLayout)findViewById(R.id.tab_Layout_);
private void changeTabsFont() {
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/"+ Constants.FontStyle);
ViewGroup vg = (ViewGroup) tab_layout.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(font);
((TextView) tabViewChild).setTextSize(15);
}
}
}
}
This code is works for me using tablayout.
It will change size of fonts and also change font style.
This will also help you guys please check this link
https://stackoverflow.com/a/43156384/5973946
This code works for Tablayout change text color,type face (font style) and also text size.
I was using Android Pie and nothing seemed to worked so I played around with app:tabTextAppearance attribute. I know its not the perfect answer but might help someone.
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabTextAppearance="#style/TextAppearance.AppCompat.Caption" />
XML FILE IN VALUES
<style name="tab">
<item name="android:textSize">#dimen/_10ssp</item>
<item name="android:textColor">#FFFFFF</item>
</style>
TAB LAYOUT
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="#dimen/_27sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_marginRight="#dimen/_10sdp"
app:layout_constraintEnd_toEndOf="parent"
app:tabTextAppearance="#style/tab"
app:tabGravity="fill"
android:layout_marginTop="#dimen/_10sdp"
app:layout_constraintStart_toStartOf="parent"
>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAB 1"
android:scrollbarSize="#dimen/_4sdp"
/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarSize="#dimen/_6sdp"
android:text="TAB 2" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarSize="#dimen/_4sdp"
android:text="TAB 3" />
</com.google.android.material.tabs.TabLayout>
> **create custom style in styles.xml** <style name="customStylename"
> parent="Theme.AppCompat">
> <item name="android:textSize">22sp</item> <item name="android:color">colors/primarydark</item>
> </style>
>
> **link to your material same name **
> <android.support.design.widget.TabLayout
> android:layout_width="match_parent"
> android:layout_height="wrap_content"
> android:id="#+id/tabs"
> app:tabTextAppearance="#style/customStylename"
> />
this is my solution
fun TabLayout.customizeTabSizeAndFont() {
val tabFont = Typeface.createFromAsset(context.assets, "font.ttf")
val tabTextSize = 21f
val viewGroup = this.getChildAt(0) as ViewGroup
for (tabVGPos in 0..viewGroup.childCount) {
val tabViewGroup = viewGroup.getChildAt(tabVGPos) as ViewGroup?
tabViewGroup?.let {
for (tabPos in 0..tabViewGroup.childCount) {
val tab = tabViewGroup.getChildAt(tabPos)
if (tab is TextView) {
tab.typeface = tabFont
tab.setTextSize(TypedValue.COMPLEX_UNIT_SP, tabTextSize)
}
}
}
}
}
Related
I need to make my TabLayout use all available space, so I use app:tabMode="fixed", app:tabGravity="fill". I want titles of my TabItems be singleline and if the titles cannot be shown in full, a part is replaced by ... (android:ellipsize = "end" behavior). I want something like this .
I try to gain it usings styles but failed.
Part of my fragment layout
`
<com.google.android.material.tabs.TabLayout
android:id="#+id/credit_details_tab_layout"
style="#style/CreditDetailsTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/credit_details_text_deposit_up_to"
app:tabMode="fixed"
app:tabGravity="fill"
android:maxWidth="0dp"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
android:layout_marginHorizontal="#dimen/spacing_M">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/information"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/history"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/management"/>
</com.google.android.material.tabs.TabLayout>
`
from styles
<style name="CreditDetailsTabLayout" parent="Widget.Design.TabLayout">
<item name="background">#color/black_for_tabs</item>
<item name="tabRippleColor">#android:color/transparent</item>
<item name="android:textAlignment">center</item>
<item name="tabBackground">#color/white</item>
<item name="tabTextAppearance">#style/CreditDetailsTabLayoutTextAppearance</item>
</style>
<style name="CreditDetailsTabLayoutTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">16sp</item>
<item name="textAllCaps">false</item>
<item name="singleLine">true</item>
<item name="textColor">#color/black_for_tabs</item>
<item name="android:ellipsize">end</item>
<item name="maxLines">1</item>
and finally screen
Can I achieve this without using CustomView ?
i am using databinding to change the color of the selected tab of tab layout
#BindingAdapter(value = ["tabIndicatorColor", "context"])
fun setSelectedTabIndicatorColor(tabLayout: TabLayout, color: Int, context: Context) {
tabLayout.setSelectedTabIndicatorColor(getColor(context, color))
}
and setting it from the tabLayout view
<variable
name="professionalTypeColor"
type="Integer" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tl_images"
android:layout_width="0dp"
android:layout_height="5dp"
tabIndicatorColor="#{professionalTypeColor}"
context="#{context}"
app:tabPaddingEnd="8dp"
app:tabPaddingStart="8dp" />
what i did until here is exactly as i wanted it to be, but for the unselected tabs i couldn't make a databinding Adapter for it so i change its color dynamically ,
i tried using
app:tabBackground="#color/grey"
or
app:tabBackground="#drawable/selector_tab_indicator"
but this need to be predefined color or a drawable with two colors (selected,unselected) which is not my desired result,
my question is how to make a databinding adapter to set the tabBackground dynamically , ( i couldn't find a setter in tablayout with attribute of tabbackground )
Create a style in style.xml and call in xml layout like below
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#color/colorAccent</item>
<item name="tabIndicatorHeight">2dp</item>
<item name="tabTextAppearance">#style/MyCustomTabTextAppearance</item>
<item name="tabSelectedTextColor">#color/colorAccent</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">#dimen/title_text_size</item>
<item name="android:textColor">#color/secondaryText</item>
<item name="textAllCaps">false</item>
<item name="android:textStyle">normal</item>
</style>
Call here
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#android:color/white"
app:tabMode="scrollable"
**style="#style/MyCustomTabLayout"**
app:tabGravity="fill" />
I would like to know that is it possible to change selected tab colour in new design tablayout? I found the solution for selected tab text color but I would like to know to change tab colour itself.
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="#style/TabLayout.Theme"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#+id/tabs"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
<style name="TabLayout.Theme" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#color/black</item>
<item name="tabIndicatorHeight">4dp</item>
<item name="tabTextAppearance">#style/TextAppearance.Jacksonville.Tab</item>
<item name="tabSelectedTextColor">#color/text_dim</item>
<item name="tabBackground">#color/color_heading</item>
</style>
i need to change selected tab colour like this.
you just need to set app:tabBackground attribute
app:tabBackground="#drawable/tab_selector_color"
and create a drawable file as tab_selector_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/tab_selected" android:state_selected="true"/>
<item android:drawable="#color/tab_unselected"/>
</selector>
so The complete xml code will be look like
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="android:attr/listPreferredItemHeight"
android:minWidth="0dp"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextColor="#color/white"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="#color/white"
app:tabIndicatorColor="#color/mainBlue"
app:tabBackground="#drawable/tab_color_selector"/>
hi you can make the hight of the indicator to equal the height of the tablayout so the indicator will cover the all size of the selected item in tab layout
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<!-- the color you want in selected tab -->
<item name="tabIndicatorColor">#50000000</item>
<item name="tabTextAppearance">#style/MyCustomTabTextAppearance</item>
<!-- set the indicator hieght equal to tablayout height -->
<item name="tabIndicatorHeight">60dp</item>
<item name="tabSelectedTextColor">#222222</item>
I have two question with TabLayout
1)Can i remove TabLayout highlight or change highlight color of tab layout?
2)Can i add ripple effect for tab. Each tab contain TextView i try to add custom background something like this
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="#drawable/btn_white_bg" />
</ripple>
but it doesn't work.
To remove the highlight, add the below line to your XML:
app:tabRippleColor="#android:color/transparent"
Another solution that works for me
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
app:tabMode="fixed"
app:tabGravity="fill"
android:clipToPadding="false"
android:elevation="0dp"
style="#style/MyCustomTabLayout"
android:background="#color/colorPrimary"
app:tabBackground="?attr/selectableItemBackground"
app:tabIndicatorColor="#color/app_yellow"
app:tabIndicatorHeight="4dip"
android:layout_height="wrap_content"/>
I just added following lines
android:background="#color/colorPrimary"
app:tabBackground="?attr/selectableItemBackground"
You can customize the TabLayout like this:
Make a xml file inside values MyCustomTabLayout.xml and then put these
<resources>
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabMaxWidth">#dimen/tab_max_width</item>
<item name="tabIndicatorColor">#color/black</item>
<!-- <item name="tabIndicatorColor">?attr/colorAccent</item> -->
<item name="tabIndicatorHeight">5dp</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">16sp</item>
<item name="android:textColor">?android:textColorSecondary</item>
<item name="textAllCaps">true</item>
</style>
and inside ur layout add this:
<android.support.design.widget.TabLayout
android:id="#+id/mainSlidingTab"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tool_bar"
android:background="#color/ColorPrimary" />
<!-- app:tabMode="scrollable" when many tabs -->
Alternatively, you can make the ripple transparent programmatically:
val tabs = findViewById<TabLayout>(R.id.your_tablayout)
for (n in 0 until tabs.tabCount) {
val tab = (tabs.getChildAt(0) as ViewGroup).getChildAt(n)
tab?.let {
val ripple = it.background as? RippleDrawable
ripple?.setColor(ColorStateList.valueOf(Color.parseColor("#00000000")))
}
}
This approach can also be used to set own ripple color for each tab.
I´m trying to change the toolbar Background color programmatically by doing this:
getSupportActionBar().setBackgroundDrawable(newColorDrawable(getResources().getColor(R.color.test_color_blue)));
And this is the result:
before:
After:
Some how the toolbar title still has the same background color as before.
here is my toolbar xml:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/Theme.Toolbar">
And here is the Theme:
<style name="Theme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:maxHeight">#dimen/abc_action_bar_default_height_material</item>
<item name="android:background">#color/primary</item>
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#android:color/white</item>
<item name="titleTextAppearance">#style/Theme.Toolbar.Title</item>
</style>
Change your code as follows:
toolbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/MyToolbarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
Theme / Style
<style name="MyToolbarStyle">
<item name="android:maxHeight">#dimen/abc_action_bar_default_height_material</item>
<item name="android:background">#color/primary</item>
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="titleTextAppearance">#style/Theme.Toolbar.Title</item>
<!-- No need for colorPrimary, colorPrimaryDark, colorAccent here
this should go to the AppTheme -->
</style>
Result
Before setting the new background color:
and after:
Late, but I hope it would be helpful comment
I had this item in my Activity style
<item name="android:background">someColor</item>
so when I changed toolbar color, title and menu items didn't change background.
I just removed this item and now it works perfect.
I did not have time to understand the details, but I think it might be useful to someone else.
use this to access the textview
public void changeToggleTitle() {
if (mToolbar != null) {
for(int i= 0; i < mToolbar.getChildCount(); i++){
View v = mToolbar.getChildAt(i);
if(v != null && v instanceof TextView){
TextView t = (TextView) v;
// Do the magic
}
}
}
}
In my style I changed
<item name="android:background">#color/background</item>
to
<item name="android:windowBackground">#color/background</item>
and that seems to have fixed it for me.
mToolbar.setBackgroundResource(mGameEnum.getPrimeColorRes());