4

Does anyone know of an example of how to do the page/view counter (little dots) like the one on home screen?

Like this example:

enter image description here

Thanks

Ricardo Gomes
  • 1,268
  • 2
  • 15
  • 35

1 Answers1

8

PagerTitleStrip is what you use with ViewPager to indicate what page you're on. It's something you add to your XML.

Layout XML:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

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

</LinearLayout>

And in your PagerAdapter:

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

However, PagerTitleStrip isn't very customizable, but ViewPagerIndicator is very customizable and includes the dotted indicator that you're looking for. You'll need to theme it to recreate the picture in your OP.

Circle indicator with ViewPagerIndicator:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.viewpagerindicator.sample"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />
<com.viewpagerindicator.CirclePageIndicator
    android:id="@+id/indicator"
    android:padding="10dip"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />

</LinearLayout>

Then in your code:

CirclePageIndicator mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);

ViewPagerExtensions is another open source library that offers custom views for ViewPager you might be interested in.

adneal
  • 30,484
  • 10
  • 122
  • 151
  • Thanks, that's probably exactly what I need. – Ricardo Gomes Apr 04 '12 at 09:57
  • I could not make it work, the project that on JakeWarton does not seem to be easy to configure. Do you have any other documentation on this that could help? – Ricardo Gomes Apr 05 '12 at 17:53
  • It's extremely easy, I'm sure you'll get it working. Here's a link to the [ViewPagerIndicator website](http://viewpagerindicator.com/#usage) and a link to [a series of articles on customizing it.](http://blog.stylingandroid.com/archives/537) – adneal Apr 05 '12 at 18:34
  • I meant the CirclePageIndicator, I didn't try the default ViewPageIndicator. – Ricardo Gomes Apr 05 '12 at 19:58
  • I gave an example of using `CirclePagerIndicator` in my post. I don't understand what you're having trouble with. And as far as documentation goes, I only know what I google. – adneal Apr 05 '12 at 21:20
  • Yes, and I appreciate your help. But the problem is that using CirclePageIndicator implies referencing the project you linked to, and thats were I'm having problems. I got the project from that github link and I cant seem to get it working, it seems to be referencing different versions of the compactability lib because most of the overrides dont seem to match the super classes. But I'll look into the rest of the documentation you provided. – Ricardo Gomes Apr 05 '12 at 23:29
  • Hmmm. [I think this link may help you.](http://stackoverflow.com/a/9469619/420015) – adneal Apr 05 '12 at 23:53
  • @aneal can u look over this link http://stackoverflow.com/questions/13813623/adding-drawable-to-circlepageindicator – Rao's Dec 11 '12 at 05:42