Android: how to close drawer with onBackPressed while drawer is locked - android

I have a pretty standard DrawerLayout in my app. In this particular app I need the drawer to only open with an icon click, not swiping, so the drawer lock mode is set to LOCKED_CLOSED unless the user presses the proper icon.
To close the drawer I want the user to be able to use the drawer icon, the home icon on the action bar OR the native Back button. The first two work just fine, but the back button really doesn't. Here's the code:
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
drawer.closeDrawer(GravityCompat.END);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}else{
super.onBackPressed();
}
}
What am I doing wrong? Should I be calling the set lock mode methods on a drawer layout object instead of the drawer?

Related

Navigation drawer not opening from cutom menu button

I'm working on an Android App, I have a navigation drawer over there. Since the navigation drawer toolbar can't be transparent, and the ending three dots button icon can't be changed, I opted for hiding that toolbar, and show my custom layout. It will give me all the functionality what ever is needed.
But the problem I'm facing right now is, once the activity starts, if I click the custom menu button it doesn't open. Once I drag it and open, after that whenever I click the menu button it opens the navigation drawer.
What might i be missing? This is what I'm doing, while debugging its even coming to the else part, but doesn't open.
In BaseActivity:
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ivLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (drawer.isDrawerOpen(Gravity.LEFT)) {
drawer.closeDrawer(Gravity.LEFT);
} else {
drawer.openDrawer(Gravity.LEFT);
}
}
});
In any of the child activity:
toolbar.setVisibility(View.GONE);
navigationView.setVisibility(View.GONE);
Please help..
The root cause of your problem is the fact that you're setting the drawer View's visibility to GONE. The direct cause of the odd behavior you describe, though, is due to how DrawerLayout and one of its helper classes update the child Views when the drawer state changes.
The OnClickListener you set to open and close the drawer was working as it should. It just didn't appear to be, since the drawer View was GONE. When you manually opened the drawer by dragging, however, the ViewDragHelper that DrawerLayout uses was firing a callback method that explicitly sets the drawer to VISIBLE. This callback is not fired when the drawer is opened programmatically - that is, with the openDrawer() method - which explains why the drawer didn't show just by clicking your custom toggle button. After you had dragged the drawer open once, the drawer View was visible, and the toggle would then work as expected.
The drawer View is in its closed state by default, so you don't need to hide it, and you can just remove the navigationView.setVisibility(View.GONE); line.

Android : How to hide Navigation bar and Optionmenu in login fragment?

I have an activity and 3 fragments, the fragment I display is login Fragment.I want to hide the Navigation bar Floating action button and Option Menu in the Login Fragment and display in other two fragment. How can I achieve this?
After a long research I found a solution. That is I lock the navigation drawer in the Login fragment using the code :
DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
And again using the code below to unlock the navigation drawer in the Welcome fragment.
DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);
This is same for Option Menu as well.

Make navigation drawer close for the first time run

I have an app with navigation drawer. Everything works good, however my navigation drawer opens automatically as my app starts.
I do not want to see my navigation drawer unless I swipe of click to menu button.
Thanks in advance!
When you launch the navigation drawer activity. Make sure it has been already opened.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
...
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
//drawer is open
}
Call YourDrawerLayout.closeDrawers(); in your main activity, on OnCreate method, to close navigation drawer.
For closing..
private void closeDrawer() {
if (mDrawer.isDrawerOpen(GravityCompat.START)) {
mDrawer.closeDrawer(GravityCompat.START);
}
}

NavigationView and ActionBarDrawerToggle

With the new NavigationView is it still recommended to use ActionBarDrawerToggle or is this not "Material Design"? For instance previously we were supposed to hide action bar items when the drawer was opened but now the guidelines say that they should stay.
With the new NavigationView is it still recommended to use ActionBarDrawerToggle
No, it's not required.
If you look at the "official" demo code for the new Design Library, ActionBarDrawerToggle is no longer used, since the new NavigationView and AppCompatActivity don't really need it.
With the new v22 support library, you can strip out all of your ActionBarDrawerToggle code and just use the following to handle the interaction between NavigationDrawer and the ActionBar/ToolBar hamburger icon:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);
...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
....
}
return super.onOptionsItemSelected(item);
}
You will need to provide your own "hamburger" drawable (R.drawable.ic_menu in my example). Besides that, the above code is all that's needed to handle opening of the drawer. The android.R.id.home case in onOptionsItemSelected() represents your hamburger drawer button. It points to a built-in resource id (not something you add to you menu xml), and it's handled automatically.
Besides that, you have to implement closing of the drawer by simply adding closeDrawers() to your click listener, like this:
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Handle menu item clicks here.
drawerLayout.closeDrawers();
return true;
}
});
closeDrawers() is a method of DrawerLayout, and takes care of everything. That's it. That's all the code you really need to properly handle navigation drawers now. No more messy code for flipping hamburgers and such!
Of course, if you really want to, you can still use NavigationView with ActionBarDrawerToggle the old way. But you certainly don't have to.
If you want drawer callbacks
Even though ActionBarDrawerToggle is not required to open/close the drawer, it may still be useful for handling additional callbacks (especially if you're using ActionBar already). Otherwise, you can implement your own by using DrawerLayout.DrawerListener or by using DrawerLayout.SimpleDrawerListener(), to handle other open/close related events.
With the new NavigationView is it still recommended to use ActionBarDrawerToggle
Yes. The two tackle two completely different aspects of the navigation drawer.
In total, there are generally three components to a navigation drawer:
A DrawerLayout
Your navigation drawer content
A method of showing and hiding the drawer
The DrawerLayout is the layout that holds the navigation drawer content and your app's content. It is what allows you to pull the drawer in from the side and display the drawer over your app's content (the first child of the DrawerLayout).
Your navigation drawer content (the second child of your DrawerLayout) is typically a list of items that the user can click on. Previously, most implementations that I have seen used a ListView or a RecyclerView and maybe a header of some sort. NavigationView is a replacement for this, and is used to provide Material-compliant drawer contents.
ActionBarDrawerToggle is used to provide the hamburger icon in your app bar. It is what allows your users to tap on the icon to open or close your drawer.
Completing the other answers, the Navigation View should be fit into the whole screen in terms of height so it will hide the hamburger icon when opened. Because of this, having the animation from burger to arrow or even just showing the arrow is not necessary.
But when clicking on the current screen it goes to another fragment, imagine a gallery of photos and clicking on a photo will show it bigger, there should be an animation from burger to arrow and the arrow should stay and when pressed there should be a reverse animation to the burger so the navigation view can be opened again.
You can achieve this with ActionBarDrawerToggle still, even with navigation view because it uses the same DrawerLayout as before. So it still has uses, but of course not necessary.

Can we launch navigation drawer without using ActionBarDrawerToggle?

Can we launch navigation drawer without using ActionBarDrawerToggle? In my implementation, I do not want to use action bar.
So, I don't want ActionBarDrawerToggle to open and close the drawer. I want to open the navigation drawer when hardware menu button is clicked.
I am targeting Android 2.2 and using support library v4.
As per my understanding, ActionBarDrawerToggle class will have implementation internally to handle the opening and closing of navigation drawer. Am I correct?
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ImageView ivNavigationIcon = //some image or button or menu what ever you want
ivNavigationIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
});
I want to open the navigation drawer when hardware menu button is clicked
Not every Android device has a MENU button, so you better have some other solution beyond just that.
Also, using MENU to open a navigation drawer, instead of a menu -- may be viewed as odd behavior by some of your users.
I am targeting Android 2.2 and using support library v4.
If you are using DrawerLayout, it has openDrawer() and closeDrawer() methods that you can use that will open the drawer and close the drawer, respectively.

Categories

Resources