0

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];
    }
}
Ephraim Schmitt
  • 227
  • 2
  • 16
  • This is too much code, try to narrow down your question to some manageable size (especially the code). – Patru Aug 04 '15 at 14:45
  • Ok, I trimmed down the code and added a cliff notes. Do you have any suggestions Patru? – Ephraim Schmitt Aug 04 '15 at 15:07
  • Much easier to grasp, but unfortunately I know too little about Android development to be much use here. However, I could not find the spot where you try to resize the `ImageView` of your icons, maybe you should add this with a comment why you *think* it should work and how it does not? – Patru Aug 04 '15 at 15:21
  • possible duplicate of [Android SlidingTabLayout with icons](http://stackoverflow.com/questions/23420293/android-slidingtablayout-with-icons) – Jared Burrows Aug 04 '15 at 15:21
  • Jared, this is not a duplicate of that question as nowhere in that question/answer is my issue addressed. Thank for the help though – Ephraim Schmitt Aug 04 '15 at 16:25

0 Answers0