Programming Android: Map View within Tab View

Based on examples:

http://developer.android.com/guide/tutorials/views/hello-tabwidget.html

http://developer.android.com/guide/tutorials/views/hello-mapview.html

Application outline

  1. Draw two empty tabs
  2. Show map on the first tab

1. Setup – obtain maps api key

Follow Maps Add-On documentation, eventually you should obtain a key for your application.

Your key is: 0pFtdSwta8EMTfArj32ycOw2kZg0LSEqa4fUGFA

This key is good for all apps signed with your certificate whose fingerprint is:

09:27:FE:8B:32:A5:AB:49:2A:30:97:0A:67:7A:EE:E2

Here is an example xml layout to get you started on your way to mapping glory:

<com.google.android.maps.MapView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="0pFtdSwta8EMTfArj32ycOw2kZg0LSEqa4fUGFA"/>

Check out the API documentation for more information.

2. Layout

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@android:id/tabhost"
   android:layout_width="fill_parent" android:layout_height="fill_parent">
   <LinearLayout android:orientation="vertical"
     android:layout_width="fill_parent" android:layout_height="fill_parent">
      <TabWidget android:id="@android:id/tabs"
         android:layout_width="fill_parent" android:layout_height="wrap_content"/>
      <FrameLayout android:id="@android:id/tabcontent"
         android:layout_width="fill_parent" android:layout_height="fill_parent">
          <RelativeLayout android:id="@+id/emptylayout1" android:orientation="vertical"
             android:layout_width="fill_parent" android:layout_height="fill_parent"/>
          <TextView android:id="@+id/textview2"
             android:layout_width="fill_parent"
               android:layout_height="fill_parent"
             android:text="Details Details Details Details"/>
      </FrameLayout>
   </LinearLayout>
</TabHost>

Maptabview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/maptablayout" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <com.google.android.maps.MapView android:id="@+id/mapview"
       android:layout_width="fill_parent" android:layout_height="fill_parent"
       android:clickable="true"
        android:apiKey="0pFtdSwta8EMTfArj32ycOw2kZg0LSEqa4fUGFA"/>
</RelativeLayout>

Application code

This would be application entry point after we'll enhance this code with more details
AppMain.java 
package com.kroz.tag;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class AppMain extends TabActivity  {
 TabHost mTabHost;
 FrameLayout mFrameLayout;

 /** Called when the activity is first created.*/
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mTabHost = getTabHost();
    TabSpec tabSpec = mTabHost.newTabSpec("tab_test1");
    tabSpec.setIndicator("Map");
    Context ctx = this.getApplicationContext();
    Intent i = new Intent(ctx, MapTabView.class);
    tabSpec.setContent(i);
    mTabHost.addTab(tabSpec);
    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Details").setContent(R.id.textview2));
    mTabHost.setCurrentTab(0);
 }
}

MapTabView.java

package com.kroz.tag;

import android.os.Bundle;
import com.google.android.maps.MapActivity;

public class MapTabView extends MapActivity {
 @Override
 protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.maptabview);
 }
 @Override
 protected boolean isRouteDisplayed() {
   return false;
 }
}

Manifest (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.kroz.tag" android:versionCode="1" android:versionName="1.0">
 <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="com.google.android.maps"/>
    <activity android:name=".AppMain" android:label="@string/app_name">
       <intent-filter>
           <action android:name="android.intent.action.MAIN"/>
           <category android:name="android.intent.category.LAUNCHER"/>
       </intent-filter>
    </activity>
    <activity android:name="MapTabView" android:label="@string/mapview_name">
       <intent-filter>
          <category android:name="android.intent.category.EMBED"></category>
          <action android:name="android.intent.action.MAIN"></action>
       </intent-filter>
    </activity>
  </application>
 <uses-sdk android:minSdkVersion="3"/>
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
</manifest>

Get example sources: http://www.kroztech.com/res/android_cookbook/src/MapTabViewDemo.zip

42 Comments

  1. Cool solution. Have to replace the formatted quotation marks(“) with unformatted ones(“) though, if you copy and paste from the blog page. Apart from that it worked like a charm.

  2. Hello Sir…

    Thank you for the code for MapActivities on Tabs…
    I is trying to make a application which on tap on map marker will give me the details on textview on the other tab….like in the foto you have shown…
    If you has such code… it would be very kind of you to post it or mail me the same…..
    Forgive my english, it is poor

    regards
    Arvind

  3. Thank you so much, I am looking for this sample for a long time, It really fix my problem, by the way, If can post AndroidManifest.xml that will be great…
    any way, I just want to say thank you so much for this Post.

    Regards
    Powen

    1. Thanks for a warm feedback.
      Actually AndroidManifest.xml is posted here – see last topic of this post

      Regards
      – Vladimir

  4. hi can u provide sample code on,how to load or get image in to from our local file to our android application,not from the drawble folder,i need to get from any local file to my emulater.

    Thank you
    Surender

  5. Hi. I am, getting a log error while running this:
    “Could not find class MapTabView.class referenced from method AppMain.onCreate”

    I have included both files in the activity element of the Manifest.

    Is there something else I can check to solve this path problem?

    1. Whenever I run this (using 2.0) I get :-

      E/AndroidRuntime( 225): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.

    2. Make sure the uses-library line: uses-library android:name=”com.google.android.maps”
      is within the application tags in the manifest.xml

      1. Make sure you use new MapView(this, MAP_API_KEY) and not something like new MapView(getApplicationContext(), MAP_API_KEY) and you shouldn’t get that error about the instances of mapactivity

  6. Nice Post, i like the article in your blog…
    i will visit this blog more often…
    Nice info in there…

    specially about
    Programming Android: Map View within Tab View

    cheers

  7. I am having problem with Map within taB. As a standalone it worked fine but within tab it is creating problem saying security exception.

    my block of code –

    locationTab = tabHost.newTabSpec(“locations”)
    .setIndicator(“Locations”, getResources().getDrawable(R.drawable.ic_tab_location))
    .setContent(new Intent(Intent.ACTION_VIEW, Uri.parse(currentLocation + “?q=” + mRetailerName)));

  8. Hi

    First of all, thanks for excellent tutorial and explanation, this blog is the best I’ve experienced, thank you again,

    I’m trying to create an app with multiple tabs with RSS feed, but I’m stuck, I’m not sure how to incorporate RSS feed (list view) into a tabbed view, if you could give me some advice, I would really appreciate it,

    Thanks

    1. The basic idea of TabView implementation is the following:
      1) Each tab has associated activity, which is defined by calling TabSpec.setContent()
      2) Your layout must contain FrameLayout element with predefined id “@android:id/tabcontent”
      3) Whenever tab is pressed, associated activity is created and its view is shown within FrameLayout mentioned above
      This is generic rule. I believe it should work for ListView as well.

      1. Hi thanks for the post, I have a question on how to jump back from 2nd activity(Details) in a tabhost to 1st activity (Map) and vice versa.
        Thanks,
        Rushi

  9. Kudos to you! The tutorial and post was crystal clear. I couldn’t figure out how to get my map inside a tab and this example worked perfectly.

  10. Pingback: Prohand
  11. I am currently in a project where we have a TabHost with multiple activities, where one tab is a MapActivity. One of the other tabs (an ordinary Activity) has a button in it, which when pressed is supposed to make the TabHost switch to the MapActivity and and animate to a specific location.

    In some tutorial I read, there was a ListActivity which when pressing on an item in the list switched to the maptab and animated to that location, but that solution is too simplistic for me.

    Anyone know how to solve this?

  12. rror: Error: No resource found that matches the given name (at ‘label’ with value ‘@string/mapview_name ‘).

    how to fix this error?

  13. The link to the source files is broken!!
    Also, your code didn’t work for me but it doesn’t matter =)

Leave a reply to Emil Cancel reply