Reputation: 14299
I have SupportMapFragment and I need to add custom controls into it for changing a map type. Calling getView(), I get NoSaveStateFramelayout and I don't think it is a good idea to add it directly into it or its children.
What is the best way how can I can add a button over my map for changing the map type?
Upvotes: 13
Views: 11537
Reputation: 241
Use this code to add you layout above of the map
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mapView = super.onCreateView(inflater, container, savedInstanceState);
@SuppressLint("InflateParams")
FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fragment_map, null);
view.addView(mapView, 0, new RelativeLayout.LayoutParams(-1, -1));
return view;
}
Upvotes: 1
Reputation: 331
The view returned from onCreateView of MapFragment is a FrameLayout. So let use it, no need to add another layout (we will reduce view hierarchy).
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
FrameLayout mapView = (FrameLayout) super.onCreateView(inflater, viewGroup, bundle);
mapView.add(someView);
return view;
}
Upvotes: 1
Reputation: 5881
I did the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<!--suppress AndroidDomInspection -->
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:src="@android:drawable/ic_menu_mylocation"
/>
</RelativeLayout>
Upvotes: 0
Reputation: 14299
I have decided to override onCreateView and encapsulate the map in the code.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
View mapView = super.onCreateView(inflater, viewGroup, bundle);
RelativeLayout view = new RelativeLayout(getActivity());
view.addView(mapView, new RelativeLayout.LayoutParams(-1, -1));
// working with view
return view;
}
And it works as I needed.
Upvotes: 37
Reputation: 41099
Try to put your map inside of a Frame Layout with Relative layout together when the map and the relative layout match_parent for the width and the height. In the relative layout place all your desired extended controls. That way the will sit on top on the map.
some thing like that:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/title_gray_background" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:text="@string/tasks"
android:paddingRight="3dp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="@color/my_even_darker_gray" />
<Button
android:id="@+id/bLocateMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginRight="3dp"
android:background="@drawable/locate_me_button_selector"
android:clickable="true"
android:onClick="locateMeButtonOnClick"
android:text="@string/locate_me"
android:textColor="@color/my_white" />
</RelativeLayout>
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Upvotes: 5