0

I've tried to add the MapView (org.osmdroid.views.MapView) in a relativeLayout in the Fragment's onActivityCreated. But it don't work. This Fragment is launched by a ShrlockFragmentActivity. How Can I make it work?

public class CommuneFragment extends SherlockFragment {

Context mContext = getActivity();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState) ;
    return inflater.inflate(R.layout.commune_map_fragment, container, false);
}


@Override
public void onActivityCreated(final Bundle savedState) {
    super.onActivityCreated(savedState);
    mContext=getActivity();

    View mView = getView();

    //Create the OSM view
    MapView mapView = new MapView(mContext, null);
    //enable zoom in and out on the OSM
    mapView.setClickable(true);
    mapView.setBuiltInZoomControls(true);

    RelativeLayout RL= (RelativeLayout) getActivity().findViewById(R.id.rl);
    RL.addView(mapView);
  }
SeyedPooya Soofbaf
  • 2,654
  • 2
  • 29
  • 31
user1298799
  • 37
  • 1
  • 1
  • 8

3 Answers3

2

That's because you have not provided the map view with width and height before adding in Relative layout :

RelativeLayout RL= (RelativeLayout) getActivity().findViewById(R.id.rl);
    RL.addView(mapView);

all you need is provide the mapview with width and height as :

this.mMapView.setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Solved my similar problem with this. Thanks

Muhammad Omer
  • 229
  • 2
  • 11
0

I found my response in this question : OSMDroid simple example required

But I don't know it doesn't work with addView

Community
  • 1
  • 1
user1298799
  • 37
  • 1
  • 1
  • 8
0

You can simple return a map view in onCreateView method.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//init mapView...
return mapView;
}

Then, in your fragmentActivity where you make de fragmentTransaction, you put this view on the content framelayout.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.container, new CommuneFragment());
            ft.commit();

sorry for my english and i hope it helps.