Search code examples
javaandroidandroid-layoutopenstreetmaposmdroid

Buttons over Mapview


I am creating an OSMdroid mapview in my MapActivity.java and I want to add buttons & pop-ups - I only know how this is done in .xml but since this MapView is not using any .xml I am puzzled how I can place (image)buttons within my java code.

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Setup base map
    final RelativeLayout rl = new RelativeLayout(this);

    CloudmadeUtil.retrieveCloudmadeKey(getApplicationContext());

    final MapView osmv = new MapView(this, 256);

    myMapController = osmv.getController();  

    rl.addView(osmv, new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    osmv.setBuiltInZoomControls(true);
    osmv.setMultiTouchControls(true);

    myLocationoverlay = new MyLocationOverlay(this, osmv);

//*snip* setup of map, mapcontrollers, tiles etc...
    osmv.getOverlays().add(tilesOverlay);
    osmv.getOverlays().add(myLocationoverlay);  

    this.setContentView(rl);
}

edit: I am talking about a button like

<ImageButton
    android:id="@+id/map_goto_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/goto_location"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" 
    android:id="@+id/goto_location" />

Solution

  • Thanks to the hint of MH I found out how to programatically add an ImageButton to my code as below:

        ImageButton goto_location = new ImageButton(this);
        goto_location.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showMylocation();
            }           
        });
    
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
        params.rightMargin = 10;
        params.topMargin = 10;
        rl.addView(goto_location, params);
    

    If anyone can hint me on some good tutorials/examples on customizing the button and on programmatically adding UI elements I would be very happy.