Mikelbstek
Mikelbstek

Reputation: 61

Sliding menu toggle option

I want to use the menu button on android to make the sliding menu toggle from left to right. The problem I face is that since I have used the sliding menu functionality in my main activity on create method, I do not how to use the same variable in the onPrepareOptionMenu method.

SlidingMenu menu;
    menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.LEFT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
    menu.setShadowWidth(10);
    menu.setBehindOffset(60);
    menu.setFadeDegree(0.25f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
    menu.setBehindWidth(400);
    menu.setMenu(R.layout.menu_frame);

this is the code which I use to call the sliding menu, However, I want to enable the toggle button whenever the menu button is called along side the swipe gesture .

public boolean onPrepareOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main, menu);
    //try to enable the toggle here so that the sliding menu can appear/disappear
    return true;
}

The problem is that unlike most cases, I do not extend my main class with the Sherlock activity since my main class is already extending some other activity. Hence I use the sliding menu in form of a constructor(look at my example). I am not sure how to integrate the toggle function. Thank for all the help

Upvotes: 1

Views: 1839

Answers (1)

SBotirov
SBotirov

Reputation: 14128

If you need override OptionsMenu methods and you want this methods public your Activity, then you first create Activity to Options menu and in you activity need extends CustomOptionMenuActivity. Example: 1. Create CustomOptionMenuActivity:

public class CustomOptionMenuActivity extends Activity {
    private Menu SlidingMenu;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.SlidingMenu = menu;
        return true;
    }

    public boolean onPrepareOptionsMenu(Menu menu) {
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.activity_main, menu);
       //try to enable the toggle here so that the sliding menu can appear/disappear
       return true;
    }
}

Then you can use menu in any activities, but you need extends this activity. Example:

public class MainActivity extends CustomOptionMenuActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       // TODO Auto-generated method stub
    }
}

Good luck!

Upvotes: 1

Related Questions