Tuesday, September 1, 2009

Side-by-side.. The lazy Android method..

One of the coolest things that the Android has out of the box is the "side-by-side" screens that make up the home screen. For a project that I am working on, it seems like this type of layout would be really useful. So, I set out trying to find out how to do it.

A bit of Googling found this article. I grabbed the code, threw it in Eclipse, and tried to make it work. Along the way Eclipse pointed out to me that the AbsoluteLayout has been deprecated. Because of that, I decided not to spend a bunch of time trying to figure out why my first few attempts using the code failed.

A bit more looking around found the HorizontalScrollView class. Using that class, along with a little creativity allowed me to make something that let me have a single really wide screen. But, what I wanted was something that snapped to the left or right to keep a single "screen" in focus.

The previously mentioned article had the code I needed to catch the events of a user dragging their finger across the screen. So, all I had to do was figure out how to put it all together. The end result is the HorizTwo class, which creates two "virtual screens" that snap to the left and right based on how far the user drags.

You can find the code on my download page here.

This code is really simple, and makes some potentially bad assumptions about the size and format of an Android screen. But, it seems like a good place for someone to start if they wanted to be able to have "side-by-side" screens.

There is one key point that may not jump out at you when you first look at the code. When you create the layouts that you want to show in each of the virtual screens, you need to give the layout an id. Normally, the Eclipse tools won't do this, so you will have to manually hunt the id field down, and set it. If you don't do this, you may not get the results you want.

1 comment:

  1. I tried to download your code but the link is broken. Any chance of re-posting, I'm trying to do exactly what you mentioned and would love to have a look at what you've done.

    ReplyDelete