I want to set the title of the TabLayout with two different text sizes. Like the given image below. Or the other way around to achieve this!
I have tried with SpannableString like give below. This snippet is in the for loop till 5!
SpannableString mSpannableString= new SpannableString(s);
mSpannableString.setSpan(new RelativeSizeSpan(2f), 0,5, 0); // set size
mSpannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
mTabLayout.getTabAt(i).setText(mSpannableString);
But as mentioned by CommonaSware setText() is not taking the rich content!
TabLayout
's default style for itsTextView
s uses aTextAppearance
with thetextAllCaps
attribute set totrue
. The transformation method for this handles theCharSequence
as a flatString
, so anySpannable
info is lost.To prevent this, we can create a style for the
TabLayout
that disablestextAllCaps
. For example:Setting this as the
tabTextAppearance
on theTabLayout
will allow yourSpannableString
to work as expected.As mentioned in comments, using a custom
View
for the tabs is another option here, since that wouldn't have the problematic attribute setting by default.