Programming Android: Map View within Tab View

Based on examples:

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:


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


Check out the API documentation for more information.

2. Layout


<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android=""
   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:text="Details Details Details Details"/>


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    android:id="@+id/maptablayout" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    < android:id="@+id/mapview"
       android:layout_width="fill_parent" android:layout_height="fill_parent"

Application code

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

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.*/
 public void onCreate(Bundle savedInstanceState) {
    mTabHost = getTabHost();
    TabSpec tabSpec = mTabHost.newTabSpec("tab_test1");
    Context ctx = this.getApplicationContext();
    Intent i = new Intent(ctx, MapTabView.class);

package com.kroz.tag;

import android.os.Bundle;

public class MapTabView extends MapActivity {
 protected void onCreate(Bundle icicle) {
 protected boolean isRouteDisplayed() {
   return false;

Manifest (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns: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=""/>
    <activity android:name=".AppMain" android:label="@string/app_name">
           <action android:name="android.intent.action.MAIN"/>
           <category android:name="android.intent.category.LAUNCHER"/>
    <activity android:name="MapTabView" android:label="@string/mapview_name">
          <category android:name="android.intent.category.EMBED"></category>
          <action android:name="android.intent.action.MAIN"></action>
 <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>

Get example sources:

Programming Android: Map View within Tab View

41 thoughts on “Programming Android: Map View within Tab View

  1. geh says:

    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. Arvind says:

    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


  3. Powen says:

    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.


    1. vkroz says:

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

      – Vladimir

  4. surender says:

    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

  5. Jig says:

    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. Clive says:

      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. Josh says:

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

      1. joe says:

        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


  7. Ranjan says:

    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. Arcanum says:


    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,


    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. Rushi says:

        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.

  9. Emil says:

    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?

  10. Jessica says:

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

    how to fix this error?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s