Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does someone know how to use PagerTitleStrip in Android

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...

like image 921
user1026605 Avatar asked Dec 21 '11 22:12

user1026605


2 Answers

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.

like image 54
Roman Minenok Avatar answered Nov 01 '22 02:11

Roman Minenok


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.

like image 26
pzulw Avatar answered Nov 01 '22 01:11

pzulw