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