How to open sliding menu on Button's click event?
Asked Answered
I

1

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 ?

Interstitial answered 20/3, 2014 at 10:41 Comment(3)
Killer Try menu.toggle() in your button click. It will open the menu if it is not open.Hookworm
...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.Coleencolella
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.Interstitial
C
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;
  }

}
Coleencolella answered 20/3, 2014 at 11:1 Comment(2)
@CodeError, the link is dead.Interstitial
@Interstitial Sir . I have fixed bugs.Which is little issue .Thats why deleted .Thanks for your kind attention (y) .Happy coding :)Girdler

© 2022 - 2024 — McMap. All rights reserved.