Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open navigation drawer on button click in main fragment?

I have made an app with one activity which uses a navigation drawer to open a number of different fragments. I have the actionbar drawertoggle, but it is not very visible. If I place a button in the onCreateView in my main fragment(the fragment that appears when my app first starts up), how can I get it to open the navigation drawer controlled by my activity?


This seems to work. The answer is much simpler than I thought it would be.

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {     View fragView = inflater.inflate(R.layout.mainmenu, container, false);     button1 = (Button) fragView.findViewById(R.id.button1);              mDrawerLayout = (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);     mDrawerList = (ListView)getActivity().findViewById(R.id.left_drawer);     button1.setOnClickListener(this);     return fragView; }   @Override public void onClick(View v) {                 mDrawerLayout.openDrawer(mDrawerList);                } 

Thank you for your answers.

like image 722
user2729524 Avatar asked Oct 18 '13 06:10

user2729524


People also ask

How do I open a navigation drawer?

The user can view the navigation drawer when they swipe a finger from the left edge of the activity. They can also find it from the home activity (the top level of the app) by tapping the app icon (also known as the Android "hamburger" menu) in the action bar.

How do I add a navigation drawer to an existing activity?

To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.

How do I customize my navigation drawer?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to res/layout/nav_header_main.


1 Answers

if you need open the slide:

mDrawerLayout.openDrawer(Gravity.LEFT); //Edit Gravity.START need API 14 

if you need close the slide

mDrawerLayout.closeDrawer(Gravity.LEFT); //Edit Gravity.START need API 14 

EXAMPLE

my mDrawerLayout is instanced here:

mDrawerLayout = (DrawerLayout)findViewById(R.id.my_drawer_layout); 

my slide state:

mSlideState=false; 

if you need to know the slide menu state (closed, opened). Use this code:

mDrawerLayout.setDrawerListener(new ActionBarDrawerToggle(this,      mDrawerLayout,      R.drawable.ic_menu_slide,     0,     0){ @Override public void onDrawerClosed(View drawerView) {                            super.onDrawerClosed(drawerView);     mSlideState=false;//is Closed } @Override public void onDrawerOpened(View drawerView) {                            super.onDrawerOpened(drawerView);     mSlideState=true;//is Opened }}); 

finally. You can use your click event like this:

public void clickEventSlide(){ if(mSlideState){                     mDrawerLayout.closeDrawer(Gravity.END); }else{                   mDrawerLayout.openDrawer(Gravity.END); }} 

In my case, my slide menu is at the right (Gravity.END), but if you need it on the left, try with Gravity.START

like image 96
Miguel Rodríguez Avatar answered Oct 07 '22 18:10

Miguel Rodríguez