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
- Draw two empty tabs
- 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 detailsAppMain.javapackage 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
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.
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
my email id is arvindisgr8@gmail.com
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
Thanks for a warm feedback.
Actually AndroidManifest.xml is posted here – see last topic of this post
Regards
– Vladimir
hi thank you so much,ur sample code solveed my problem
Regards
surender
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
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?
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.
Make sure this line:
is within the application tags in the manifest.xml
Make sure the uses-library line: uses-library android:name=”com.google.android.maps”
is within the application tags in the manifest.xml
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
it helped me, thanks lot for the useful posting….
very welcome 🙂
Do you have the project running?. xle2911@hotmail.com
Can you email me?
I’ve added link to project sources
thanks for the code…good work
Strange
It works fine on Android 1.5 and 1.6
Nice inspiring blog, dude. I enjoy your posting. I am about to create blog about mobile phones, too.
Thanks!
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
Thanks, Helen
Thank you;
Good solution which I’m looking for.
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)));
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
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.
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
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.
Great example, straight to the point and easy to implement. Thanks.
finally found it. cool!
Thanks man, works like a charm
Nice thanks. realized I was missing using-library in the manifest.
wooo~~~~~~~~~ You are the best!!!!
Thank you.
actually I am finished project.
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?
rror: Error: No resource found that matches the given name (at ‘label’ with value ‘@string/mapview_name ‘).
how to fix this error?
The link to the source files is broken!!
Also, your code didn’t work for me but it doesn’t matter =)
Your code is really helpfull. can you plz send me the code . my email id is : er.ikndp@gmail.com
your code is super and helpful can you send the code to my mail id.
my mail id is sultanstd@gmail.com
Good!
Thank you very much, I do copy now.