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 ?
menu.toggle()
in your button click. It will open the menu if it is not open. – Hookwormmenu.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. – Coleencolellamenu.showMenu
ormenu.toggle()
then i am getting error likejava.lang.IllegalStateException: This SlidingMenu appears to already be attached
so aftermenu.showMenu
ormenu.toggle()
do i need to clear anything. – Interstitial