user3414107
user3414107

Reputation:

How to display ViewPager title?

Hi i have to implement ViewPager in my app. here i have to load ImageAdapter that extends PagerAdapter and i successfully see all the images into viewpager.

Now, i want to display viewpager title as my image counter.

Layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gallery_detail_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black" >

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/rel_ig" >
</android.support.v4.view.ViewPager>
</RelativeLayout>

Please some one give me information how i want to do that!

Upvotes: 1

Views: 384

Answers (2)

Namrata
Namrata

Reputation: 1684

Try This :-

add a PagerTitleStrip in activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

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

</LinearLayout>

Then in PageAdapter.java add the following method:

@Override
    public CharSequence getPageTitle(int position) {
        return "Title Here";
    }

Upvotes: 1

M D
M D

Reputation: 47817

First need to add android.support.v4.view.PagerTabStrip into your Layout.xml file like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gallery_detail_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black" >

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

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pts_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</android.support.v4.view.ViewPager>
</RelativeLayout>

Now, override CharSequence getPageTitle(int position) method int you Adapter like:

    public class ImageAdapter extends PagerAdapter {

     ............ 
     @Override
     public CharSequence getPageTitle(int position) {
         return (position + 1);
         }
    ...........
}

And implement into your PagerTabStrip like: you can customized PagerTabStrip options

PagerTabStrip strip = (PagerTabStrip)view.findViewById(R.id.pts_main);
strip.setDrawFullUnderline(false);
strip.setTabIndicatorColor(Color.DKGRAY);
strip.setBackgroundColor(Color.GRAY);
strip.setNonPrimaryAlpha(0.5f);
strip.setTextSpacing(25);
strip.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);

Upvotes: 2

Related Questions