Google recently released a couple of Android applications where it is possible to scroll through views.This has been seen before in applications such as Youtube, but two things are different. One is the indication bar telling us what we are looking at, and what we can find, if we move right or left. This is a fast, minimalistic way for the user to see what he is looking at, and also a nice indication that you can scroll sideways, and what you will get if you do. The other way this differentiates itself from the previous implementations is that Google released a library as well as the source for all to use.
Only problem is, they didn’t release the indicator, so if you want your new application to fit in with the latest and greatest from Google you are stuck with making it yourself, or finding some of the other solutions out there. Screenshot here.
We have implemented an easy to use indicator View that closely follows the behavior of the Google+ indicator. Shown on the image is the indicator used to show consecutive dates.
You add the indicator to the layout is just as you would any other:
This gives you just the line containing the titles of the pages. It is up to you to style the indicator such that it fits in to your application with additional views. The layout on the screenshot can be found in the example application.
Now, some initialization is necessary before the indicator is fully operational. Your OnCreate might look something like this:
Running init on the indicator is mandatory, as it will not otherwise have the data it needs. Adding arrow images is optional (but generally a good idea).
Finally the callback is a simple method returning the title of the page at a given position. We have opted to extend the pager to do this, returning the date for that position:
While most of the view can be styled with normal view attributes the focused and unfocused text colors can be set programmatically using the setFocusedTextColor (int) and setUnfocusedTextColor(int) methods. They take an array containing the integer representation of rgb.
Just like the compatibility library, the ViewPagerIndicator is compatible with Android SDK version 4 and up.
Download source and example code - here.
Mark Gjøl is an M.Sc from The Technical University of Denmark, the creator of the Android gallery Floating Image, and works with Android, Windows and Tomcat applications.