Android Tabs with interacting map and list views
Last tutorial, we wrote a simple app that displays two interacting list views in a TabActivity
. In this tutorial, we will up the ante and add a MapView
as the content of one of the tabs. Why again are we using multiple views in an activity instead of using a separate activity for each tab content? Remember, we want our tabs to be able to easily interact with one another, and keeping them as views allows us to handle the logic and interaction within one activity.
So, our goal in this tutorial is to have a list of geo coordinates and when we click on an item in the list, our map view goes to that location.
Step 1: Create a layout
First off, let’s create an XML layout that contains a TabHost
, TabWidget
, a ListView
, and a MapView
.
Step 2: Write the Activity Code
Once we have a layout, we need to create our main Activity. While last tutorial our Activity
was of type TabActivity
, in this tutorial our Activity
has to be of type MapActivity
.
Why is this? Using a MapView in Android must be in a MapActivity
class, otherwise your app with throw an Exception (take a look at the activities source code if you’re interested in more details). As a result of this, we will have to perform the functions of TabActivity
ourselves in the MapActivity
class (you’ll see that below).
Let’s create a class called TabbedListMapActivity
and have it extend MapActivity
.
In the onCreate()
method, we need to set the content to our XML layout, extract the TabHost
object, and call setup()
on the TabHost
(we need to do this because our Activity is not a TabActivity).
Next, we want to extract our ListView
from the XML, set it to a member variable, and add some initial coordinates to its list adapter.
Then, we want to extract our MapView
from the XML and set it to a member variable.
Once we have our MapView
, we can add a listener to the ListView
for selection events. Let’s add a listener that when an item is clicked, we set the map to the coordinates of the selected item.
The setMapZoomPoint
method is implemented like this:
The final step is to add our two views (MapView
and ListView
) as content to our tab host.
Step 3: Fix Common Issues
Now if you were to run this app now, you may notice strange behavior (might just be on my phone). You should see our two tabs starting with the list view tab. Instead you may see the MapView
“bleeding” through the ListView
. I’m not exactly sure what causes this, but a workaround is to manually set the tab view to the map, then manually set the tab view back to the list. This causes the activity to redraw the tabs correctly. Here’s my workaround in code:
Now everything should look right and you should be able to click a coordinate on the list view, and the map view tab goes to that spot on the earth.
If you are having issues with seeing the Map, remember you need to use the Google-Apis instead of the standard Android apis, you need to generate a Maps API key, you need to add interest permissions to the manifest, and you need to make sure the uses-library line is inside your application tag in the manifest.
Here’s my manifest for reference:
To see an activity with a map and list view in action, check out my Earthquake Alert! Android App
Full Source
Here’s the full Java source code: