I have created a SlidingTabLayout that I adjusting using code found here so that it can use icon instead of text. My problem is that there is just a little too much padding on the sides of the icons causing the tab strip to scroll slightly. I have 4 icons/tabs.
What I have tried I have tried adjusting ever where that I can think of to get rid of the padding or make the icons smaller.
First I adjusted imageView in the createDefaultImageView(), then I adjusted the width in my custom_tabs.xml. Then I tried adjusting the tabIconView and tabView in the populatedTabStrip(). I have also tried to setBounds() on the images before passing to the tabIconView.setImageDrawable(). Nothing seems to change. I can make the padding larger, but cannot make it smaller or decrease the size of my icons.
Any help would be appreciated. Thanks.
Cliff notes: Need to adjust icon size and/or padding in slidingtablayout
My Code:
SlidingTabLayout.java
private void populateTabStrip() {
//creating a new populateTabStrip to use icons
final TroopPagerAdapter adapter = (TroopPagerAdapter) mViewPager.getAdapter();
final View.OnClickListener tabClickListener = new TabClickListener();
for (int i = 0; i < adapter.getCount(); i++) {
View tabView = null;
ImageView tabIconView = null;
if (tabView == null) {
tabView = createDefaultImageView(getContext());
}
if (tabIconView == null && ImageView.class.isInstance(tabView)) {
tabIconView = (ImageView) tabView;
}
tabIconView.setImageDrawable(getResources().getDrawable(adapter.getDrawableId(i)));
if (mViewPager.getCurrentItem() == i) {
tabIconView.setSelected(true);
}
tabView.setOnClickListener(tabClickListener);
mTabStrip.addView(tabView);
}
}
//Added in to get the icons on the slider
protected ImageView createDefaultImageView(Context context) {
ImageView imageView = new ImageView(context);
int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
imageView.setPadding(0, 0, 4, 4);
int width = (int) (getResources().getDisplayMetrics().widthPixels / mViewPager.getAdapter().getCount());
return imageView;
}
}
custom_tabs.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabContainer"
android:layout_width="40dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tabImage"
android:layout_gravity="center"/>
<TextView
android:layout_width="42dp"
android:layout_height="wrap_content"
android:id="@+id/tabText"
android:gravity="bottom|center"/>
</LinearLayout>
TroopPagerAdapter.java
public class TroopPagerAdapter extends FragmentPagerAdapter {
int icons[] = {R.drawable.elix_barracks_tab, R.drawable.dark_barracks_tab, R.drawable.spells_king_tab, R.drawable.setup_tab};
@Override
public CharSequence getPageTitle(int position) {
return null;
}
@Override
public int getCount() {
return numberOfTabs;
}
public int getDrawableId(int position) {
return icons[position];
}
}