Search code examples
androidslidingmenujfeinstein

J. Feinstein Sliding Menu check if open


I used J. Feinstein library to make awesome sliding menu(thanks for J. Feinstein), but I need to know whether sliding menu is opened or closed. I need to know because I have implemented ABC action bar while in some condition, sliding menu opened and user can open navigation drawer and make my app dimmed(overlay from navigation drawer).

I know from documentation, slidingMenu.toggle() can automatically check condition of sliding menu, whether is closed or opened(If it is open, it will be closed, and vice versa). but please see my code bellow:

            @Override
            public void onDrawerOpened(View drawerView) {
                // here i wanna check if sliding menu opened or closed
                if(isOpened)
                  // if is opened then close the sliding menu first
                  menu.toggle();

                super.onDrawerOpened(drawerView);
                if (!isAdded()) {
                    return;
                }

                if (!mUserLearnedDrawer) {
                    mUserLearnedDrawer = true;
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(getActivity());
                    sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).commit();
                }

                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }

I want to check if sliding menu opened, then close sliding menu first and then draw navigation after that. How to do that?

Many thanks.


Solution

  • if you need to check sliding menu is opened or closed, you can use this code bellow :

    // Check whether sliding menu is opened or closed
    if(menu.isMenuShowing()){
      //Do action here
    }
    

    in my case, i need to close sliding menu first and then open navigation drawer, so here the code :

    @Override
    public boolean onCreateOptionsMenu(Menu menuActionBar) {
      if (!mNavigationDrawerFragment.isDrawerOpen()) {
        getMenuInflater().inflate(R.menu.main, menuActionBar);
        restoreActionBar();
        return true;
      }else{
        if(menu.isMenuShowing()){
          menu.toggle();
        }
      }
      return super.onCreateOptionsMenu(menuActionBar);
    }
    

    I hope this solution will help others. Thanks for Stackoverflow and J. Feinstein.