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 ?
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With