6

I am implementing sliding menu using this link. Now it is working perfectly fine if I use this code in onCreate() method.

My simple question is How can I open a slider on button's click event ?

I used following code.

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final SlidingMenu menu = new SlidingMenu(this);

        Button mButton = (Button) findViewById(R.id.slidingMenu);
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                menu.setMode(SlidingMenu.RIGHT);
                menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
                menu.setFadeDegree(0.5f);
                menu.attachToActivity(MainActivity.this,
                        SlidingMenu.SLIDING_CONTENT);
                menu.setMenu(R.layout.activity_menu);
            }
        });
    }

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

}

but when I click on a button sliding menu is not getting opened. I am not getting any error. How can I do this ?

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • 2
    Killer Try `menu.toggle()` in your button click. It will open the menu if it is not open. – Rethinavel Mar 20 '14 at 10:54
  • 2
    ...or ```menu.showMenu()``` if you want to just open the menu, but not toggle it. Also you should move all the set methods in your OnClickListener outside the on ClickListener. – peshkira Mar 20 '14 at 10:56
  • But if i use `menu.showMenu` or `menu.toggle()` then i am getting error like `java.lang.IllegalStateException: This SlidingMenu appears to already be attached` so after `menu.showMenu` or `menu.toggle()` do i need to clear anything. – InnocentKiller Mar 20 '14 at 10:58

1 Answers1

8

You should setup the menu before the on click listener. In your snippet you setup and attach the menu every time you click the button. However you should only show the menu and not attach it on click.

public class MainActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final SlidingMenu menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.RIGHT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    menu.setFadeDegree(0.5f);
    menu.attachToActivity(MainActivity.this, SlidingMenu.SLIDING_CONTENT);
    menu.setMenu(R.layout.activity_menu);

    Button mButton = (Button) findViewById(R.id.slidingMenu);
    mButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
          menu.showMenu();
          //or 
          //menu.toggle();
        }
    });
 }

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

}
peshkira
  • 6,069
  • 1
  • 33
  • 46