Choub890
Choub890

Reputation: 1163

Android menu not appearing on google maps activity (Api v2)

I have an activity in android that is supposed to show google maps using the google maps api v2, with a menu on top. The map appears like it should, but the menu never shows up for some reason.

I have debugged the application, and the onCreateOptionsMenu(Menu menu) method is never called. Why is this?

This is my activity:

public class MapsActivity extends Activity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_maps, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.menu_item_direction) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
            // Check if we were successful in obtaining the map.
                if (mMap != null) {
                setUpMap();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

This is my map layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="tp1.inf8405.itinerary.activities.MapsActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout>

Finally, this is my menu layout in res/menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_item_direction"
        android:title="@string/action_menu_directions"/>
</menu>

Upvotes: 1

Views: 5593

Answers (5)

Muhammadjon
Muhammadjon

Reputation: 188

public class AddressSelectorActivity extends **Activity**

public class AddressSelectorActivity extends AppCompatActivity

it means 'use AppCompatActivity'

Upvotes: 0

CodeSlave
CodeSlave

Reputation: 457

Extend from AppCompatActivity not ActionBarActivity

Upvotes: 0

Choub890
Choub890

Reputation: 1163

The solution was to make my activity class inherit from ActionBarActivity

Upvotes: 6

bjiang
bjiang

Reputation: 6078

I think you may change your AppTheme as follows:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

It will show the ActionBar which contains a menu in it.

Also, you can refer an example source code from my Github here.

enter image description here

Upvotes: 3

Lawrence Choy
Lawrence Choy

Reputation: 6108

In your onCreateOptionsMenu(), return true instead of calling the super method.

According to the doc, you need to return true for the menu to be displayed.

Upvotes: -1

Related Questions