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.
I have a much simpler solution using
isDrawerOpen()
.This automatically closes or opens the navigation drawer based on the drawer's current state (Opened/Closed)
if you are using from default navigation activity in android you just have to add this code in click listener of button --->
for closing you do not have to do something.
Use the standard callback model as described here:
http://developer.android.com/training/basics/fragments/communicating.html
When you press the button, initiate a callback to the activity and have it open your drawer.
if you need open the slide:
if you need close the slide
EXAMPLE
my mDrawerLayout is instanced here:
my slide state:
if you need to know the slide menu state (closed, opened). Use this code:
finally. You can use your click event like this:
in my case my slide menu is at the right (Gravity.END), but if you need on the left, try with Gravity.START
This work for me, I hope solved your problem.