How to open the navigation box by pressing a button in the main fragment?

I created a one-action application that uses a navigation box to open several different fragments. I have an actionbar drawertoggle, but this is not very noticeable. If I put a button in onCreateView in my main fragment (the fragment that appears when my application starts first), how can I make it open the navigation box, controlled by my activity?


It 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 responses.

+16
android fragment navigation-drawer
source share
6 answers

if you need to open a slide:

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

if you need to close the slide

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

EXAMPLE

my mDrawerLayout is given here:

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

my slide state:

 mSlideState=false; 

if you need to know the status of the slide menu (closed, open). 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 the click event as follows:

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

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

+61
source share

I have a much simpler solution using isDrawerOpen() .

This automatically closes or opens the navigation box depending on the current state of the box (Open / Closed)

  Button hamMenu = findViewById(R.id.ham_menu); hamMenu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DrawerLayout navDrawer = findViewById(R.id.drawer_layout); // If the navigation drawer is not open then open it, if its already open then close it. if(!navDrawer.isDrawerOpen(Gravity.START)) navDrawer.openDrawer(Gravity.START); else navDrawer.closeDrawer(Gravity.END); } }); 
+7
source share

if you use default navigation activity in android, you just need to add this code to the button click --->

 mDrawerLayout.openDrawer(Gravity.START); 

you don’t have to do anything to close.

+4
source share

To use a toolbar as an application bar, first make sure your activity goes beyond AppCompatActivity. Then call setSupportActionBar () and pass the toolbar object from your layout:

  toolbar=(Toolbar) findViewById(R.id.toolbar_main); setSupportActionBar(toolbar); ActionBar actionbar = getSupportActionBar(); actionbar.setDisplayHomeAsUpEnabled(true); actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp); drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout); ActionBarDrawerToggle actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.addDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.syncState(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: drawerLayout.openDrawer(GravityCompat.START); return true; } return super.onOptionsItemSelected(item); } @Override public void onBackPressed() { if (drawerLayout.isDrawerOpen(GravityCompat.START)){ drawerLayout.closeDrawer(GravityCompat.START); } else{ super.onBackPressed(); } } 

}

+1
source share

Use the standard callback model as described here:

http://developer.android.com/training/basics/fragments/communicating.html

When you click a button, initiate an activity callback and open it.

0
source share

Use these lines to open and close a box for a specific event:

Code snippet for opening a box:

 drawerLayout.openDrawer(Gravity.START); 

Code snippet for closing a box:

 drawerLayout.closeDrawer(Gravity.LEFT); 

openDrawer(gravity_of_navigation_view_to_be_shown)

in openDrawer ("gravity"), in the "gravity" section, you must enter the gravity of the navigation view, as described above:

 Gravity.LEFT Gravity.RIGHT Gravity.START Gravity.END 

I think this is the best answer.

0
source share

All Articles