0

I'm currently using a ViewPager with a PagerTabStrip to display 4 tabs at the top of the device. All I want to do is have 4 tabs fixed in place and centered, but I can't figure out how to do that now that the old way (using Action bar tabs) has been deprecated.

Basically I have something like this: enter image description here

But I want it so that all 4 of my tabs are on the same page (all visible).

This is my code to set the pager adapter to the ViewPager:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));

}

and this is the relevant XML:

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    tools:context=".MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_tab_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@color/theme_color"
        android:textColor="#fff"
        android:paddingTop="18dp"
        android:paddingBottom="18dp"/>

</android.support.v4.view.ViewPager>

So how would I fix the tabs using PagerTabStrip? If I can't, what else can I use to do it?

Note: similar questions have been asked before but I can't find any solid answers that work. Similar question here as well.

Community
  • 1
  • 1
u3l
  • 3,342
  • 4
  • 34
  • 51
  • Are you trying to create the material design tabs like this? https://cloud.githubusercontent.com/assets/7454787/7192840/853c99a4-e467-11e4-8259-aa45df5dfddd.PNG – Eugene H May 21 '15 at 18:26
  • Yeah, exactly. But the way I know how to do it is deprecated with Android L so I'm pretty lost – u3l May 21 '15 at 18:34
  • I will post code for you using another library I just need to know if you are using fragment as the tabs. – Eugene H May 21 '15 at 18:35
  • Thanks -- yeah, using fragments. – u3l May 21 '15 at 18:35

1 Answers1

2

If you need any more information post it in the comment below.

First you want to add the library to the project in the gradle or install the library.

Gradle add compile 'com.jpardogo.materialtabstrip:library:1.0.9' or download project at https://github.com/jpardogo/PagerSlidingTabStrip

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.eugene.pagertesting"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.jpardogo.materialtabstrip:library:1.0.9'
}

XML

<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"
    tools:context=".MainActivity">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:minHeight="56dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark"/>


    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_below="@+id/toolbar"
        android:background="@color/primary"
        android:textColorPrimary="@color/white"
        app:pstsDividerColor="@color/primary"
        app:pstsIndicatorColor="@color/white"
        app:pstsIndicatorHeight="2dp"
        app:pstsShouldExpand="true"
        app:pstsUnderlineHeight="0dp"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabs"/>

</RelativeLayout>

PagerAdapter

public class PagerAdapter extends FragmentPagerAdapter {

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

    private Fragment f = null;

    @Override
    public Fragment getItem(int position) { // Returns Fragment based on position
        switch (position) {
            case 0:
                f = new FragmentPageOne();
                break;
            case 1:
                f = new FragmentPageTwo();
                break;
        }
        return f;
    }

    @Override
    public int getCount() { // Return the number of pages
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) { // Set the tab text
        if (position == 0) {
            return "Fragment One";
        }
        if (position == 1) {
            return "Fragment Two";
        }
        return getPageTitle(position);
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {
    ViewPager viewPager;
    PagerAdapter pagerAdapter;
    PagerSlidingTabStrip pagerSlidingTabStrip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.pager);
        pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        pagerSlidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        viewPager.setAdapter(pagerAdapter);
        pagerSlidingTabStrip.setViewPager(viewPager);
    }
}

enter image description here

If you want to know how I styled the app I will post that as well just let me know.

UPDATE if you would like to drop the shadow below the tabs I can post that as well.

Eugene H
  • 3,520
  • 3
  • 22
  • 39
  • @uj- no problem. Code on! – Eugene H May 21 '15 at 19:14
  • Quick question: would you happen to know if it would be possible to nest another `PagerSlidingTabStrip` inside a particular tab, without attaching a `ViewPager` to it? I want to put a couple tabs under the first tab of the `PagerSlidingTabStrip` but I don't want to be able to swipe between them because that will interfere with the first set of tab's `ViewPager`. It's cool if you're not sure how to this though. – u3l May 22 '15 at 19:23
  • You can add a another viewpager within a viewpager. Just set the height of that viewpager to be small so you can swipe between the main pages. – Eugene H May 22 '15 at 19:56
  • If I set the height of the `ViewPager` to be small won't that hide the fragments that it pages through? – u3l May 22 '15 at 20:03
  • @EugeneH how to set only 1 tab visible at a time.. In your image you have fragment 1 and 2. I want fragment 1 to cover the entire screen.. I've followed this [tutorial](http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/) – Prabs Nov 12 '15 at 11:17
  • @Prabs So you are not looking for Tabs? – Eugene H Nov 13 '15 at 15:52
  • @EugeneH Yes, I am looking for tabs.. but in your pic you have 2 tabs visible on screen..like tab 1 and tab 2..if you swipe forward then you get tab 2 and tab 3 on screen.. Similarly I want only tab 1 visible on screen.When you swipe tab 2 should come then tab 3..but each tab should occupy the 100% of screen. In your case every tab is occupying 50% of the screen – Prabs Nov 14 '15 at 09:27
  • @Prabs that is against google design guidelines. I suggest not using tabs at all. Just use the Toolbar and viewpager. When the user swipes to the other screens, just change the toolbar title. – Eugene H Nov 14 '15 at 17:39
  • @EugeneH Do You want me to remove the tabs and listen to the swipe actions(may be by using `OnSwipeTouchListener()` ) and perform any set of methods like changing the title, changing the content etc. Is that what you are saying?? – Prabs Nov 16 '15 at 06:54