Android: SwipeView

Note from future Jason: don't use this, it's very old and very unmaintained. Official ways now exist.

There are numerous tutorials scattered across the web that claim to show you how to make a swipeable view component similar to the home screen, unfortunately none of them seem to do the key thing for affordance and usability; follow your finger as you swipe (or fling as its sometimes called)

Introducing SwipeView! What I’ve created is a view component that you can add to your project that behaves (almost) exactly like any other Android view component such as ScrollView or RelativeLayout etc. You can specify it in an xml layout file or programmatically in your java code.

I’ve also created another view component that works very nicely alongside the SwipeView called PageControl. This is something you can optionally add to your view that gives you little dots to show which page you are on. The dots work in both the built in dark and light themes (and in theory any other theme although I’ve not tested this yet). You can also change the dots to any Drawable of your choice and you can change the size of your dots.

There is also a Listener you can add that tells you when the pages have been changed so you can implement anything from a gallery that dynamically loads and unloads images as you swipe between the pages, to a news reader that only begins to download content once the user has swiped to that particular page, or anything you can imagine!

This should support all versions of Android from 1.5 all the way to 2.2 but I’ve only tested it on a G1 running 1.6 and a Nexus One running 2.2.

But how can you use it I hear you asking? To begin with I will just be releasing a compiled jar file that you can add to your project (beginners: right click on your project in Eclipse->Properties->Java Build Path->Add External Jars). I will be open sourcing it soon, I just need to learn how to do the whole open source project thing.

There are still a few bugs, for example the trackball doesn’t behave itself. I still need to work that one out. There is also a particular finger motion you can do when swiping backwards that makes the page listener action report that it is coming from a page one after it is actually coming from, e.g. you are swiping from page 5 to page 4, but it reports you are swiping from page 6 to page 4.

You can view the documentation at jasonfry.co.uk/swipeview/doc/.

Download the jar.

I’ve bundled a demo app with the jar file to show you how to use everything. If you would like to run the demo app on your phone right now you can use this amazing QR code:

QR Code lost to time..

The demo app seems to think it wants permissions for storage and phone calls, I've not requested this in the manifest so I don't really know what it thinks its doing. So don't worry, the app isn't going to ring premium rate numbers whilst deleting everything from your sd card. If you are worried, you can compile and run the app from the source contained in the bundle above.

If you use this I’d love to hear from you, tweet at me redacted. Same goes for any questions, comments or feedback in general! (or if you know why the demo app wants those extra permissions!)