Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20656

Hide ActionBar icon

I'm trying to hide the icons when my drawer nagvigation is shown. The this is that only hide the settings, and not the icon that I want to.

On my MainActivity I've seen that If I add this code the icon hides, but then If I press other fragment to show it crashes.

    @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    menu.findItem(R.id.ofertasRefresh).setVisible(!drawerOpen); <--- I want to hide this icon
    return super.onPrepareOptionsMenu(menu);
}

When I press other fragment to show it says ... :

01-09 20:03:01.493  25761-25761/info.androidhive.slidingmenu E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.androidhive.slidingmenu, PID: 25761
java.lang.NullPointerException
        at info.androidhive.slidingmenu.MainActivity.onPrepareOptionsMenu(MainActivity.java:170)
        at android.app.Activity.onPreparePanel(Activity.java:2564)
        at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:464)
        at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
        at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:543)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5102)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

Is there any way to do it easyly? By the way, on my fragment 1 I've added an onCreateOptionsMenu to show the image and it does perfectly.

 @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.refresh_menu, menu);
}

R.menu.refresh_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/ofertasRefresh"
    android:icon="@drawable/ic_action_refresh"
    android:title="Refresh"
    android:alphabeticShortcut="r"
    android:orderInCategory="200"
    android:showAsAction="ifRoom" />
</menu>

Cheers.

Upvotes: 0

Views: 131

Answers (2)

natario
natario

Reputation: 25204

Best way in my opinion is overriding onDrawerClosed() / onDrawerOpened() methods in your ActionBarDrawerToggle. See here:

public boolean mDrawerOpened;

    ActionBarDrawerToggle abdt = new ActionBarDrawerToggle( ... ) {

        public void onDrawerClosed(View v) {
            super.onDrawerClosed(v);
            mDrawerOpened = false;
            invalidateOptionsMenu();
            syncState();
        }

        public void onDrawerOpened(View v) {
            super.onDrawerClosed(v);
            mDrawerOpened = true;
            invalidateOptionsMenu();
            syncState();
        }

invalidateOptionsMenu() starts a call to the onPrepareOptionsMenu() method in the host Activity. Then:

public boolean onPrepareOptionsMenu(Menu menu) {
    if (mDrawerOpened) { menu.removeItem(R.id.ofertasRefresh); }
    if (!mDrawerOpened) { menu.add(Menu.NONE, R.id.ofertasRefresh, Menu.NONE, R.string.title); }
    return super.onPrepareOptionsMenu(menu);
}

Using visibility (probably works better):

public MenuItem mi;

public boolean onPrepareOptionsMenu(Menu menu) {
    if (mi == null) { mi = menu.findItem(R.id.ofertasRefresh); }
    if (mDrawerOpened) { mi.setVisible(false); }
    if (!mDrawerOpened) { mi.setVisible(true); }
    return super.onPrepareOptionsMenu(menu);
}

Upvotes: 2

Ero
Ero

Reputation: 520

I think it would be best if you had a menu XML for each fragment and activity. This means you won't have to hide/show certain items. Also regarding callbacks(from the docs):

The fragment also receives callbacks to onOptionsItemSelected() when a menu item is selected.

Note: Although your fragment receives an on-item-selected callback for each menu item it adds, the activity is first to receive the respective callback when the user selects a menu item. If the activity's implementation of the on-item-selected callback does not handle the selected item, then the event is passed to the fragment's callback. This is true for the Options Menu and context menus.

Upvotes: 1

Related Questions