Can we open multiple DrawerLayout in one activity.
i mean when open on one DrawerLayout and onclick on item open another drawerlayout.
(Nested Drawer layout).
Yes you can using Fragments.
From the Fragment's onCreateView() you can include a button somewhere that has OnClickListenerwhere in you put this code .
open a new Drawer :
//For me a better way in avoiding a `null pointer` in getting the DrawerLayout
final DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Opens the Drawer
drawer.openDrawer(Your View, Usually a ListView);
}
return false;
});
And close Drawer :
drawer.closeDrawer(Your View, Usually a ListView);
Hope it helps .
Related
I'm working with Navigation Drawer and I want to click the button on activity and Navigation Drawer should show, but I don't know how to do it. Normally, when we swipe from left edge to the right the navigation will be shown. as such, I want to be able to open the navigation drawer with the click of a button.
Use the following methods to open and close your navigation drawer:
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//To Open:
drawerLayout.openDrawer(Gravity.START);
//To Close:
drawerLayout.closeDrawer(Gravity.END);
Source1 for more information:
Source2
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(GravityCompat.START)) navDrawer.openDrawer(Gravity.START);
else navDrawer.closeDrawer(Gravity.END);
}
});
You can also use a method and Use the exact same steps given by #Zeeshan.
public void hamMenu(View view){
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (!drawer.isDrawerOpen(GravityCompat.START)) {
drawer.openDrawer(GravityCompat.START);
} else {
drawer.closeDrawer(GravityCompat.END);
}
}
Use this method in the button which you need android:Onclick="hamMenu" in .xml.
I have a navigation drawer in my BaseActivity. Now I want to detect the NavigationDrawer state(Opened or Closed) in a separate fragment. I tried in Fragment life cycle methods, but no method is triggered when NavaigationDrawer is opened or closed. Any one Please help me. Thanks in advance.
You can use this line of code in your fragment that you want to check if the drawer is closed or not:
DrawerLayout mDrawerLayout = (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);
ListView mDrawerList = (ListView) getActivity().findViewById(R.id.list_slidermenu);
if (!mDrawerLayout.isDrawerOpen(mDrawerList)){
System.out.println("The Drawer is closed!");
}
Basically you are calling the layout from the activity that is hosting that fragment.
You have to implement DrawerListener in baseactivity,
BaseActivity implements DrawerListener
Now you have to assign
yourNavigationDrawerObject.setDrawerListener(this);//please put your object behalf of yourNavigationDrawerObject
call when navigation drawer close.
#Override
public void onDrawerClosed(View arg0) {
// TODO Auto-generated method stub
}
call when navigation drawer open
#Override
public void onDrawerOpened(View arg0) {
// TODO Auto-generated method stub
}
in your navigationdraweractivity make drawer layout as public
public DrawerLayout drawerLayout;
In your Fragment
NavigationsDrawerActivity _activity =(NavigationsDrawerActivity)this.Activity;
then add your events
this._activity.drawerLayout.DrawerOpened += drawerLayout_DrawerOpened;
this._activity.drawerLayout.DrawerClosed += drawerLayout_DrawerClosed;
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.
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
You Should Use isDrawerOpen()
The piece of code below automatically closes or opens the navigation drawer based on the drawer's current state (Opened or 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);
}
});
if you are using from default navigation activity in android you just have to
add this code in click listener of button --->
mDrawerLayout.openDrawer(Gravity.START);
for closing you do not have to do something.
Use these lines to open and close the drawer on a certain event:
Code snippet for opening drawer:
drawerLayout.openDrawer(Gravity.START);
Code snippet for closing drawer:
drawerLayout.closeDrawer(Gravity.LEFT);
→ openDrawer(gravity_of_navigation_view_to_be_shown)
in openDrawer("gravity"), in "gravity" section, you have to input the gravity of the Navigation View like given above:
Gravity.LEFT
Gravity.RIGHT
Gravity.START
Gravity.END
I think thats the best answer.
To apply the toolbar as the app bar, first make sure your activity extends from 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();
}
}
}
The simplest way in my opinion
#Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(findViewById(R.id.navigationViewId))){
mDrawerLayout.closeDrawer(Gravity.LEFT);
}else
super.onBackPressed();
}
I'm working on application that has a tab structure, and use sliding movements to move through the tabs.
But now, I want to apply Drawer Layout. The problem is that the Drawer has slide to open events. How I can delete this event? My idea was that the Drawer only could open and close with a button. Is this possible?
Thanks!
Just write
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
to prevent menu from listening to gesture
and use openDrawer and closeDrawer to change menu visibility
By default the DrawerLayout is initially hidden from the view unless you put a code to open the Drawer, by the time there is a sliding event triggered.
From the Navigation Drawer example, the contain content_frame is used to dynamically display views inside the Drawer using fragments.
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
From the Fragment's onCreateView() you can include a button somewhere that has OnClickListener where in you put this code,
//For me a better way in avoiding a `null pointer` in getting the DrawerLayout
final DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Opens the Drawer
drawer.openDrawer(Your View, Usually a ListView);
}
return false;
});
You Can also use* to close the drawer.
drawer.closeDrawer(Your View, Usually a ListView);
you can write this way
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawer.openDrawer(navigationView);
}
});
If you want to navigate between drawer item on click of button inside your fragments,
you can use this
((YourMainActivity)getActivity()).selectItem(position);
I am using navigation drawer. I have hidden the action bar using aandroid:theme="#android:style/Theme.Light.NoTitleBar". So i am able to slide the drawer using swipe only. I want to create a button. On button click i want to show the drawer. I have googled but couldn't find much. Thanks in advance.
Call openDrawer on your onClick:
public void openDrawer (View drawerView)
Open the specified drawer view by animating it into view.
Parameters drawerView Drawer view to open
http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html#openDrawer(android.view.View)
drawer.openDrawer(Gravity.LEFT);
and to close :
drawer.closeDrawer(Gravity.LEFT);
Try this:
final ImageView toolbar1 = (ImageView) findViewById(R.id.Toolbar1);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toolbar1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawer.openDrawer(GravityCompat.START);
}
});