Mani
Mani

Reputation: 71

How to call this function to OnClick Event?

How to call this function to OnClick Event? want to call onCreateOptionMenu by using onclick event or using button onclick event so kindly help me for this issue?

    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.option_menu, menu);

      switch (mWhichRouteProvider){
     case OSRM: 
        menu.findItem(R.id.menu_route_osrm).setChecked(true);
        break;
    case GRAPHHOPPER_FASTEST:
        menu.findItem(R.id.menu_route_graphhopper_fastest).setChecked(true);
        break;
    case GRAPHHOPPER_BICYCLE:
        menu.findItem(R.id.menu_route_graphhopper_bicycle).setChecked(true);
        break;
    case GRAPHHOPPER_PEDESTRIAN:
                       menu.findItem(R.id.menu_route_graphhopper_pedestrian).setChecked(true);
        break;
    case GOOGLE_FASTEST:
        menu.findItem(R.id.menu_route_google).setChecked(true);
        break;
    }

    if (map.getTileProvider().getTileSource() == TileSourceFactory.MAPNIK)
        menu.findItem(R.id.menu_tile_mapnik).setChecked(true);
    else if (map.getTileProvider().getTileSource() == TileSourceFactory.MAPQUESTOSM)
        menu.findItem(R.id.menu_tile_mapquest_osm).setChecked(true);
    else if (map.getTileProvider().getTileSource() ==     MAPBOXSATELLITELABELLED)
        menu.findItem(R.id.menu_tile_mapbox_satellite).setChecked(true);

    return true;
}

Upvotes: 0

Views: 277

Answers (4)

ImAtWar
ImAtWar

Reputation: 1143

Why not use :

@Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menuitem1: dosomefunction();
        break;
    }

To me this seems to be a better way

Upvotes: 0

Rich Sala
Rich Sala

Reputation: 93

Just take all the code in onCreateOptionsMenu and put it in another function(doTheThing())

then you have

public boolean onCreateOptionsMenu(Menu menu) {
    doTheThing(menu);
return true
}

then call do the thing in an onClickListener as well

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157457

you can use invalidateOptionsMenu. From the documentation

Declare that the options menu has changed, so should be recreated. The onCreateOptionsMenu(Menu) method will be called the next time it needs to be displayed.

The method is available from api level 11.

Upvotes: 2

Ravi
Ravi

Reputation: 35569

use following line in your onClick event

 openOptionsMenu();

Upvotes: 2

Related Questions