I am new to Android Development and I would like to know what is the easiest way that i can insert google maps in fragments. All guides I have found online are rather difficult for a beginner-level developer.
All help is appreciated.
this guide is pretty simple and easy to work with. Try it out.
https://github.com/codepath/android_guides/wiki/Google-Maps-Fragment-Guide
Google Maps is integrated in your app using the SupportMapFragment class. So you see, map itself is type of fragment. Now the question really is, how to insert fragments into your activity.
You can do it via XML or progrmatically. For XML insertion, you can insert this code into your XML :
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then in your java file, you would get a refrence of this fragment like this :
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.googleMapFragment);
In case you have your own custom fragment and would like to insert a map fragment into your custom fragment, you will follow the same strategy above but will have the XML and Java code in your respective custom fragment files.
Related
My old eclipse version worked perfectly but after updating eclipse, when I create a new Android project, it creates two XML layouts. 1st is activity_main.xml and second is fragment.xml. So when i add some item in activity_main.xml it wont display anything.
So how to use fragment layout?
This is the new Project Structure for Android by developers.
If you want to view any UI widgets put them inside fragment.xml file.
And any java code should put inside PlacementHolder class that is inner class of Main.java.
As others mentioned that's just the recent new project style from Android Team. But I guess you were also looking for an introduction into Fragments when you asked:
So how to use fragment layout?
Basically:
Android team has completely covered Fragments in their documentation. Start from here and then refer here. Then download this official source code and examine it closely.
Now I'm gonna share a personal experience:
I suggest you first study this tutorial on fragments from Vogella's website.
Why? Because the Android documentation is slow and rather boring there fore hard to understand, whereas the Vogella version is to-the-point and sharp.
You can then go back and study Android documentation.
at least read this: http://developer.android.com/guide/components/fragments.html it will give you an idea how the fragments work. And better switch now to Android Studio, the sooner the better.
I have an app having actionbar where one of the fragments contain a mapFragment. Also, i've to make an overlay of list on top of the map. MapFragment as of now as lesser functionality(i cant make setInfoWindowAdapter to work in it).So my question is should I be using Mapview within a fragment or MapFragment within in a fragment? which is the best practice?
I've read through many articles but none them exactly mention which is the best option and why.
Both are correct if done correct.
Fragments inside fragments are supported as of support library v11.
Two things to remember:
you cannot put MapFragment to xml
you have to use getChildFragmentManager
Check this link for how to correctly add MapFragment inside your fragment: http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1
I want to display Map in one of my Fragment I am using 4 fragments in which one shows the map but i don't want to use LocalActivityManagerto host a Activity inside a fragment. It is a deprecated class, but it offers the simplest solution.
By using MapActivity it is starting a new Activity which i don't want i want to display it in viewpager and MapView dont work in Fragment.
Please suggest me how to get it.
Thanks for Help.
Since Maps V1 (MapActivity and kin) is deprecated, you could consider moving to the new Maps V2, which has MapFragment (for native API Level 11 fragments) and SupportMapFragment (for the Android Support Library's backport of fragments).
I found no other way then the one mentioned in the following way...
https://stackoverflow.com/a/8126287
I have created a 3-tabbed layout using ActionBarSherlock library, 2 of which containing maps. I have also added some annotations on the maps. From now on I will refer to the one of the two tabs containing maps but the same will apply for the other one, too.
What I want to do is to "refresh" the contents of the map. This will be done when clicking on an annotation that ideally will open a list fragment containing details about it and that will replace the map fragment entirely.
So, I want to do this "switching" between map and list fragment but I can't because the only way I found in order for the tabs to change content is by registering the ActionBar.TabListener.
http://developer.android.com/reference/android/app/ActionBar.TabListener.html
Moreover, this is not a solution because there is no tab event fired in this case, as far as I am concerned.
Now I'm building the tabs and their contents programmatically.
Would it be easier to create it with xml files in order to have direct access to the tabs? Now, I don't have a "pointer" to the tab objects.
If so, any guidelines about this? I' m referring to the tabs, not the action bar menu.
Thanks in advance, for any suggestion.
As much as i understand your question, I think you should make use of SharedPreferences to pass on the values from one fragment to another while on tabclick...http://developer.android.com/reference/android/content/SharedPreferences.html
As an exercise, I am trying to rewrite the following google tutorial with Fragment class. The original tutorial implements tabs by using the old TabActivity class and TabHost/TabWidget annotation.
Tab Layout Google Tutorial
I have converted all Activity class with Fragment. I couldn't make my new code to work. I think I am stuck.I could not find any 'complete' Tab sample code using Fragment class.
Here are my questions
1. Should I define in the res/layout/main.xml or calling Actionbar.addTab(...) in my entry class, or both?
2. What would be complete res/layout/main.xml looks like? What would be the root element (i.e. LinearLayout, FrameLayout...etc)?
3. Any additional info would be greatly appreciated.
Check out this example from the compatibility library demos: FragmentTabs.java
and the corresponding layout: fragment_tabs.xml
Really, though, I wouldn't start with Tabs if you're trying out Fragments for the first time. Tabs in Android are a little bit of a mess. The above example (from Google itself) uses a hack just to get things working. Tabs just add a layer of unnecessary confusion when you're just learning.
Here's a more straightforward starting-out Fragments example/tutorial: http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
(Just make sure to replace things like getFragmentManager() with getSupportFragmentManager() if you're using the compatibility library.)