Foushi
Foushi

Reputation: 375

Fragment Tabs not using all space

I'm trying to convert my activity app into a fragment following this tutorial : http://www.exoguru.com/android/material-design/navigation/material-design-tabs-with-fragments As I will use only two tabs, I have made this change to BlankFragment

public class BlankFragment extends Fragment {

    private PagerSlidingTabStrip tabs;
    private static ViewPager viewPager;
    private final int int_items = 2;

    public BlankFragment() { // Required empty public constructor 
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View x = inflater.inflate(R.layout.fragment_blank, container, false);

        tabs = (PagerSlidingTabStrip) x.findViewById(R.id.tab_strip);

        viewPager = (ViewPager) x.findViewById(R.id.viewpager);

        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

        tabs.post(new Runnable() {
            @Override
            public void run() {
                tabs.setViewPager(viewPager);
            }
        });

        int[][] tabStates = new int[][] {
                new int[] { android.R.attr.state_pressed}, // enabled
                new int[] { android.R.attr.state_selected},  // unchecked
                new int[] { -android.R.attr.state_selected}
        };

        int[] tabColors = new int[] {R.color.colorAccent,R.color.colorAccent,R.color.colorAccent};

        ColorStateList tabList = new ColorStateList(tabStates, tabColors);
        tabs.setTextColor(tabList); // Setting the Tab Text Color

        return x;
    }

    public class MyAdapter extends FragmentPagerAdapter { // Tab Adapter

        public MyAdapter(FragmentManager fm) { super(fm); }

        @Override
        public Fragment getItem(int position)
        {
            switch (position){
                case 0 : return new Tab1();
                case 1 : return new Tab2();
            }

            return null;
        }

        @Override
        public int getCount() { return int_items; }

        @Override
        public CharSequence getPageTitle(int position) {

            switch (position){
                case 0 :
                    return "Temps        ";
                case 1 :
                    return "Vetement       ";
            }
            return null;
        }
    }

}

The thing is that I get this result : enter image description here

As you can see the tabs text is not using all the space like this : https://raw.githubusercontent.com/joseedwin84/Android-Sliding-Tabs-With-Material-Design/master/tabpic.png

So what does I need to change ? I tried to check layout and they are all set to match_parent width. So now I don't know where to look. If someone can help me, It would be great.

Upvotes: 1

Views: 664

Answers (2)

Collins Abitekaniza
Collins Abitekaniza

Reputation: 4578

You have to set the tabGravity to fill and tabMode to fixed of your TabLayout.

Either in xml

<android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout"
        app:tabGravity="fill"
        app:tabMode="fixed" />

Or you can do it in java

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);

Upvotes: 3

Foushi
Foushi

Reputation: 375

Thanks I'm not using TabLayout but a PagerSlidingTabStrip, and with your answer I found in the documentation that it was a layout problem I should use app:shouldExpand="true"

Upvotes: 0

Related Questions