Question

I'm building an Android app using the AppCompat_v7 library and I'm having troubles with the action bar.

I have an AppBarActivity which extends ActionBarActivity - I'm using the same actionbar for all of my activities. MainActivity extends AppBarActivity. I've defined four actionbar icons, two of which I'd like to be displayed at a time. I've been careful to define my own namespace, ie. app:showAsAction="always"/>

In my app, I toggle the visibility of these icons in onPrepareOptionsMenu()

MenuItem contactOn = menu.findItem(R.id.contact_toggle_button_on);
MenuItem contactOff = menu.findItem(R.id.contact_toggle_button_off);
contactOn.setVisible(!useContacts);
contactOn.setEnabled(!useContacts);
contactOff.setVisible(useContacts);
contactOff.setEnabled(useContacts);

Do I need to specify a namespace here somehow? Because it doesn't seem that these methods are doing anything, except, curiously, rearranging their names in the overflow list. Also, only three of the four buttons show up there, which I don't understand either but I expect that's a different problem.

UPDATE: posting code & xml

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
     MenuInflater inf = new MenuInflater(this);
      inf.inflate(R.menu.main, menu);
      return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {        
    MenuItem contactOn = menu.findItem(R.id.contact_toggle_button_on);
    MenuItem contactOff = menu.findItem(R.id.contact_toggle_button_off);
    contactOn.setVisible(!useContacts);
    contactOn.setEnabled(!useContacts);
    contactOff.setVisible(useContacts);
    contactOff.setEnabled(useContacts);

    MenuItem locationOn = menu.findItem(R.id.location_toggle_button_on);
    MenuItem locationOff = menu.findItem(R.id.location_toggle_button_off);
    locationOn.setVisible(useLocation);
    locationOn.setEnabled(useLocation);
    locationOff.setVisible(!useLocation);
    locationOff.setEnabled(!useLocation);
    return true;
}

And the XML

  <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.example.skortchm.strangerapp.MainActivity" >

        <!-- Contacts Toggle, should appear as action button -->
        <item
            android:id="@+id/contact_toggle_button_on"
            android:icon="@drawable/ic_action_call"
            android:title="@string/contact_toggle_on"
            app:showAsAction="always"/>
        <item
            android:id="@+id/contact_toggle_button_off"
            android:icon="@drawable/ic_action_call_off"
            android:title="@string/contact_toggle_off"
            app:showAsAction="always"/>

        <!-- Location Toggle, should appear as action button -->
        <item
            android:id="@+id/location_toggle_button_on"
            android:icon="@drawable/ic_action_location_searching"
            android:title="@string/location_toggle_on"
            app:showAsAction="always"/>
        <item
            android:id="@+id/location_toggle_button_off"
            android:icon="@drawable/ic_action_location_off"
            android:title="@string/location_toggle_off"
            app:showAsAction="always"/>

        <!-- Settings, should always be in the overflow -->
        <!-- Add gear icon here -->
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:title="@string/action_settings"
            app:showAsAction="never"/>

    </menu>
Was it helpful?

Solution

You need to use getMenuInflater to inflate the menu.

You are creating an instance for MenuInflater, that is not right . you have to use the instance associated with the Activity

Change your onCreateOptionsMenu as

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top