The setup
I have an activity whose contentView is an instance of a DrawerLayout
, which has a navigation drawer with a drawer indicator displayed in the action bar. The activity contains a Fragment
, let's call it ListFragment
, which contains a list of options. When an option is clicked, I replace the ListFragment
with a DetailFragment
.
At this point, I would like to display an "up" navigation option instead of the navigation drawer indicator. I'm able to display the "up" icon if I disable the drawer indicator by calling mDrawerToggle.setDrawerIndicatorEnabled(false)
, but this only removes the drawer icon--it does not remove the functionality--that is, when I click the caret, the navigation drawer is still opened.
Additionally, in these subviews, I would like to disable the opening of the drawer by dragging from the edge of the screen. I have tried doing this by calling setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
but it doesn't seem to have disabled this functionality.
I have tried extending the ActionBarDrawerToggle
class to prevent opening the drawer when the indicator is clicked--however, all that happens is that the overriding action (the "up" navigation) is performed, but the drawer still opens.
I have also implemented the steps in Switching between Android Navigation Drawer image and Up caret when using fragments . It works insofar as displaying the caret goes, but despite overriding the up button functionality, the menu still opens (the app does navigate back--it just also opens the drawer).
Question
So, long story short: is there any (preferably clean and elegant, but at this point I'll go with hacky) way to achieve these things when my layout root is a DrawerLayout
:
- Replace the drawer indicator with an "up" caret (tentatively doable via
mDrawerToggle.setDrawerIndicatorEnabled(false))
- Prevent the drawer from opening when the caret is clicked, and instead override with my own "up" functionality
- Prevent the drawer from opening when I drag from the edge of the screen.
Edit
All right, it looks like if I both override ActionBarDrawerToggle
AND onOptionsItemSelected
, the menu does not open when I click the caret. But it still opens if I drag from the edge. Help!
This is only part of the solution that I arrived at, but it was quite hard to figure out this bug, so I'm leaving this here for posterity's sake.
This how I was defining the ListView for my navigation drawer:
Even after calling
setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
I was still able to slide the drawer open.However, after changing the
layout_gravity
to"start"
this problem seems to be resolved.I was able to reproduce this issue in a sample, navigation-drawer-only app, so it does appear to be a reproducible issue not unique to my situation.
Building on answer by @sonida And after using the tweaks given by @luca992 and @jai.
I tried above suggested codes But the "up" or "Back" arrow in left side of action bar was just not showing up in my app. But luckily I was able to fix that.
I had to add this extra line of code in setNavigationDrawerState() [Ref: android.support.v7.app.ActionBarDrawerToggle.setHomeAsUpIndicator ]
I downloaded the drawable: ic_keyboard_backspace_white_24dp from Material.io
Here is the complete code:
MainActivity.java -> onCreate()
MainActivity.java -> setNavigationDrawerState()
MainActivity.java -> onBackPressed()
MainActivity.java -> startFragment() [dummy function for example]
MyFrag.java --> onViewCreated()
MyFrag.java --> onDestroyView()
You need to disable swipe and disable the actionbar home button:
Use the below code that builds on the code already given to disable swipe
Short Code
Building on sonida's answer. After calling setDrawerIndicatorEnabled(false), onNavigateUp wasn't being called still. So, I just created a new onClickListener that called it:
also I think
has been depreciated, but it works fine without it.