22

I decided to use a ViewPager in my application and everything is working fine. I Know that I want to use a PagerTitleStrip in my ViewPager, but I've failed to find any info on how to do it... The one and only page (sic!!) I found on this class is http://developer.android.com/reference/android/support/v4/view/PagerTitleStrip.html So it seems I just need to add the PagerTitleStrip within my ViewPager layout, but I don't see anything new on my activity...

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

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pagerTitleStrip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />
</android.support.v4.view.ViewPager>

Does anyone know how to use it?

Edited: Sorry it works when implementing the getPageTitle method in the ViewPagerAdapter... but I don't want any text displayed, just a small cursor to show the position of the current View compared to the previous and next ones...

Jethro
  • 928
  • 1
  • 10
  • 20
user1026605
  • 1,633
  • 4
  • 22
  • 58

3 Answers3

46

I'm not sure what causing error in your case but this is how I use it, in your layout you should insert following code in your layout:

<android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

        <android.support.v4.view.PagerTitleStrip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top" />
</android.support.v4.view.ViewPager>

Then you should add the following code in your PageAdapter:

        @Override
        public CharSequence getPageTitle (int position) {
            return "Your static title";
        }

That's pretty it.

CrimsonX
  • 9,048
  • 9
  • 41
  • 52
Roman Minenok
  • 9,328
  • 4
  • 26
  • 26
  • It works pretty well... it's not the best in terms of customization but it does its job. This should be the accepted answer! – andrea.rinaldi Sep 10 '15 at 08:21
2

I had this issue too where the PagerTitleStrip was not visible. The answers above did not help. The problem was that I followed the example on http://developer.android.com/reference/android/support/v4/view/ViewPager.html.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);
    setContentView(mViewPager);

The code above shows the view pager but not the PagerTitleStrip.

I changed the code to

protected void onCreate(Bundle savedInstanceData) {
    super.onCreate(savedInstanceData);
    setTitle(R.string.my_title);

    setContentView(R.layout.my_pager);
    viewPager = (ViewPager)findViewById(R.id.pager);

res/layout/my_pager.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />

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

I was also able to be backward compatible to API 8 by removing all the code in the ViewPager (javadoc page) example to manipulate the ActionBar and ActionBar.Tab.

pzulw
  • 1,716
  • 15
  • 22
-1

PagerTitleStrip is a non-interactive signal of the current, next, and former pages of a ViewPager. It really is designed to be used as a child view of a ViewPager widget

Step 1. Create layout activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidviewpagerapp.MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    <android.support.v4.view.ViewPager
        android:id="@+id/myviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/titlestrip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.v4.view.ViewPager>
     </LinearLayout>

Step 2. Create Activity contains ViewPager this (MainActvity.java)

public class MainActivity extends Activity {
 ViewPager viewPager;
 MyPagerAdapter myPagerAdapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  viewPager = (ViewPager)findViewById(R.id.myviewpager);
  myPagerAdapter = new MyPagerAdapter();
  viewPager.setAdapter(myPagerAdapter);
  PagerTitleStrip pagerTitleStrip = (PagerTitleStrip)findViewById(R.id.titlestrip);
 }
}

Demo : http://photo-wonder.blogspot.com/2016/09/pagertitlestrip-on-viewpager-android.html

tienanhcntt2
  • 139
  • 1
  • 3