Search code examples
androidnavigation-drawerslidingdrawer

Android Navigational Drawer activate w/ button


since i am quite new to Android i am trying to create a Navigational Drawer that gets activated when pressing a button. I checked several tutorials like this one but in all of them the drawer gets activated when an Actionbar button is pressed. One more thing. My activity consists of 3 buttons and an image so i use RelativeLayout, how can i add a FrameLayout to RelativeLayout? Should i change it?

Can someone work something out so i can get the general idea? Thank you in advance for your time!


Solution

  • In the XML element (e.g. Button, or TextView) put:

    android:onClick="onBtnClick"
    

    In the linked Activity add the method:

    public void onBtnClick(View view) {
        //open the drawer as a menu
        View mFragmentContainerView = findViewById(R.id.navigation_drawer);
        DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.openDrawer(mFragmentContainerView);
    }
    

    You can simply drag a FrameLayout into a RelativeLayout, so I do not understand your issue there.