Android development is partially about having a set of tools in your UI toolbox that you bring out when appropriate in the design and implementation phases of the project. I’m about to add an invaluable tool to your toolbox. Meet the SwipeView.
What is a “SwipeView”?
A SwipeView is an Android UI view similar to the homescreens. You have several similar panes of content that you access by swiping left and right. As you swipe left and right, transitions between the views are animated so that the view in focus follows your finger. See the handy illustration below (Click to enlarge):
When Should I use a “SwipeView”?
SwipeViews are useful if you’re going to have several screens with the same layout and function that you want the user to switch between. If you’ve ever used the Epicurious app they make good use of a SwipeView-esque implementation when searching for recipes. Think of SwipeViews as something like a Gallery implementation for Views instead of just images.
How Do I implement a “SwipeView”?
Here’s the fun part. Now that you know you want a SwipeView in your Android application how do you implement it. As you probably know its not a standard Android View. As such we’re going to make use of a third party library. Jason Fry has put together a nice library for the simple implementation of SwipeViews. Head on over to his site and blog post for more details about how to implement your very own SwipeView. Since each SwipeView implementation is going to be widely different, I’m just going to summarize the steps to implement his library below:
- Add SwipeView element (<uk.co.jasonfry.android.tools.ui.SwipeView…) to the desired XML layout as you would any Android View.
- Reference SwipeView in java code via findViewById() as usual
- Populate SwipeView with number of views that you’d like to be able to swipe between
- Implement method private class SwipeImageLoader implements OnPageChangedListener {public void onPageChanged(int oldPage, int newPage) {}}. Look at the library’s documentation and examples for more details on how to do this. Essentially this method will handle dynamically allocating and populating views as your user swipes back and forth.
I would include an example of implementation here, but my implementation was quite unorthodox and borrowed ideas from how to implement ListViews and other portions of my Android experience. Needless to say its a bit more involved than what should be expected from an introduction to an UI pattern. If you’d like help implementing your own SwipeView, feel free to drop a line in the comments below or give us a shoutout via twitter @droidweb.
