Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Pager Sliding Tab Strip with two categories equally fit in screen android?

I am using pager sliding tab strip where I am getting extra space at right side. How to equally fit for two categoreis in tabs?

My code is as follows:

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
    tabs.setIndicatorColor(Color.BLUE);
    tabs.setIndicatorHeight(2);
    mPager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();
    ViewPagerAdapter adapter = new ViewPagerAdapter(fm);
    mPager.setAdapter(adapter);
    tabs.setViewPager(mPager);
    tabs.setShouldExpand(true);

        public class ViewPagerAdapter extends FragmentPagerAdapter {

    // Declare the number of ViewPager pages
    //final int PAGE_COUNT = 2;

    public ViewPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }
    private final String[] TITLES = { "SIGN IN", "REGISTER"};

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

and my xml is :

  <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.views.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    android:background="@drawable/background_tabs" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:background="#000000"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/colors"
    android:layout_below="@+id/tabs"
    tools:context=".MainActivity" />

Here is my screenshot

enter image description here

like image 814
Shadow Avatar asked Dec 26 '22 01:12

Shadow


2 Answers

Instead of adding setShouldExpand in java file, i think will have to add it in xml file... like this ..

<com.views.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="@drawable/background_tabs" 
app1:pstsShouldExpand="true"/>

also add xmlns:app1="http://schemas.android.com/apk/res/your.package.name" in your parent layout like this..

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app1="http://schemas.android.com/apk/res/your.package.name" >

Hope it works...!!

like image 174
iMDroid Avatar answered Dec 27 '22 16:12

iMDroid


Add

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.weight = 1;
tabView.setLayoutParams(lp);

To the populateTabStrip() method in SlidingTabLayout. It should be looks like:

private void populateTabStrip() {
        final PagerAdapter adapter = mViewPager.getAdapter();
        final OnClickListener tabClickListener = new TabClickListener();

        for (int i = 0; i < adapter.getCount(); i++) {
            View tabView = null;
            TextView tabTitleView = null;

            if (mTabViewLayoutId != 0) {
                // If there is a custom tab view layout id set, try and inflate it
                tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
                        false);
                tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
            }

            if (tabView == null) {
                tabView = createDefaultTabView(getContext());
            }

            if (tabTitleView == null && TextView.class.isInstance(tabView)) {
                tabTitleView = (TextView) tabView;
            }

            tabTitleView.setText(adapter.getPageTitle(i));
            tabView.setOnClickListener(tabClickListener);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.weight = 1;
            tabView.setLayoutParams(lp);
            mTabStrip.addView(tabView);
        }
    }
like image 39
Govtart Avatar answered Dec 27 '22 14:12

Govtart