I am using design support library to create tabs. By default it creates tabs with text. How can I create tabs with only icons? Also I want to change the icons color if it's current selected tab.
Asked
Active
Viewed 5,130 times
3
-
Maybe, this answers will help: http://stackoverflow.com/a/7656618/2649012 – Phantômaxx Aug 23 '15 at 10:22
4 Answers
8
Use this to populate the viewPager:
public class SectionPagerAdapter extends FragmentPagerAdapter {
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return mFragmentA;
case 1:
return mFragmentB;
case 2:
return mFragmentC;
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
return ""; // This removes the title, as you wish
}
}
And then this in onCreateView:
final TabLayout tabLayout = (TabLayout) v.findViewById(R.id.tab_layout);
final ViewPager viewPager = (ViewPager) v.findViewById(R.id.view_pager);
viewPager.setAdapter(new SectionPagerAdapter(getActivity().getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
Also convenient to know:
for (int i = 0; i < tabLayout.getTabCount(); i++) {
int iconId = -1;
switch (i) {
case 0:
iconId = R.drawable.icon1;
break;
case 1:
iconId = R.drawable.icon2;
break;
case 2:
iconId = R.drawable.icon3;
break;
}
tabLayout.getTabAt(i).setIcon(iconId);
}
// Needed since support libraries version 23.0.0
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tabLayout.getTabAt(position).select();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
}
});
And to change the selected tab color:
tabLayout.setSelectedTabIndicatorColor(colorId); // For the line
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).getIcon().setTint(getResources().getColor(R.color.gray));
}
tabLayout.getTabAt(selectedTabPosition).getIcon().setTint(colorId);
}

Dante
- 1,104
- 1
- 10
- 15
-
How to change only single tab background color like tabLayout.setSelectedTabIndicatorColor(colorId); this changes strip color. – Shishram Oct 21 '15 at 10:44
-
-
@MuneerahRashed you can get it from the tabLayout, something like this `tabLayout.getSelectedTabPosition()` – altfuns May 05 '18 at 21:08
2
You can return empty titles in the view pager adapter,or in case you need to reuse the adapter just follow setting the icons by setting the titles to empty like this:
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
TabLayout.Tab tab = mTabLayout.getTabAt(i);
tab.setIcon(R.drawable.icon);
tab.setText("");
}

Saif C
- 421
- 4
- 10
1
There's no direct way I know of. But going through the documentation I noticed that you could set a Tab
's icon with setIcon()
So maybe you do something like this:
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
mTabLayout.getTabAt(i).setIcon(R.drawable.icon);
}
You can also do the a setCustomView()
on Tabs it seems.
Do something like this:
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
View v = inflater.inflate(view, parent, false);
((ImageView) v).setImageResource(R.drawable.icon);
mTabLayout.getTabAt(i).setCustomView(v);
}

Rakeeb Rajbhandari
- 5,043
- 6
- 43
- 74
-
I tried setIcon but still title is visible. The getPageTitle method of view pager adapter is showing the title in tabs. – Nitesh Kumar Aug 23 '15 at 10:11
-
how about after not setting any titles in the view pager adapter ? – Rakeeb Rajbhandari Aug 23 '15 at 10:12
1
You can get the "icon only" effect by using a SpannableString and adding an ImageSpan to it. See: How can i develop the PagerSlidingTabStrip with images in android?