I am trying to find a way to change text size of Tab Title
when selected. Until now without exit. Hope someone can help me.
My code bellow:
XML
You can set your own view to the TabLayout's individual tabs and you can change the size latter on the tab selection-
Here is the code hint -
TabLayout mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
TabLayout.Tab tabOne = mTabLayout.newTab();
tabOne.setCustomView(getLayoutInflater().inflate(R.layout.item_tab, mTabLayout, false));
mTabLayout.addTab(tabOne);
TabLayout.Tab tabTwo = mTabLayout.newTab();
tabTwo.setCustomView(getLayoutInflater().inflate(R.layout.item_tab, mTabLayout, false));
mTabLayout.addTab(tabTwo);
tabTwo.select();
// mTabLayout.setupWithViewPager(mViewPager);
if (getResources().getDisplayMetrics().widthPixels > getResources().getDisplayMetrics().heightPixels) {
mTabLayout.setTabMode(TabLayout.MODE_FIXED);
} else {
mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
}
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
((TextView) tab.getCustomView().findViewById(R.id.text1)).setTextSize(16);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
((TextView) tab.getCustomView().findViewById(R.id.text1)).setTextSize(13);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
iteb_tab.xml can be like -
You can further synchronize the selection with viewpager page change as
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mTabLayout.getTabAt(position).select();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Edit
You can further reduce your effort by setting your tab titles from the adapter itself -
PagerAdapter mPagerAdapter = mViewPager.getAdapter();
for (int position = 0; position < mPagerAdapter.getCount(); position++) {
View view = (getLayoutInflater().inflate(R.layout.item_tab, mTabLayout, false));
TextView label = (TextView) view.findViewById(R.id.text1);
label.setText(mPagerAdapter.getPageTitle(position));
TabLayout.Tab tab = mTabLayout.newTab();
tab.setCustomView(view);
mTabLayout.addTab(tab);
}
Here is how it looks -