I have a Navigation Drawer (appcompat v7) in my app which is working perfectly fine.
Now I want to disable it, until the user buys an in-app-purchase to unlock additional functionality. So in my Activity.onCreate(), after initializing the drawer and populating it, I am calling this function:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
This function is not doing anything. The drawer continues to open and close as normal after tapping the drawer carat in the actionbar. I tried calling this function in Activity.onResume() without any difference.
What is the correct way to use this function?
(I tried looking online for answers, but couldn't find anything which addresses my issue). Any help is appreciated, as I am stuck on this issue for quite sometime now.
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
is only disabling the opening drawer layout by swiping till you click navigation drawer icon
keep a boolean variable
write mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); in onStart() and also write below lines of code
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
if(!disabled)
{
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerLinearLayout)) {
mDrawerLayout.closeDrawer(mDrawerLinearLayout);
} else {
mDrawerLayout.openDrawer(mDrawerLinearLayout);
}
}
}
return super.onOptionsItemSelected(item);
}
this will work for sure
When you call setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) it locks opening and closing drawer only by swipes.
The drawer continues to open and close as normal after tapping the drawer carat in the action bar because your drawer will still respond to calls to openDrawer(int), closeDrawer(int) although a drawer is locked.
You need to add some logic in your action bar menu button listener and not to call openDrawer(int) when you don't want it to open.
Btw, it is okay to call setDrawerLockMode(int) in onŠ”reate
There is a bug with DrawerLayout and used gravity. I have reported it here:
https://issuetracker.google.com/issues/136738274
Related
I have a DrawerLayout that usually works perfectly. I am using a button that opens the drawer by calling this method:
mDrawerLayout.openDrawer(mFragmentContainerView);
As I said, everything works fine but sometimes when the activity comes back from background the openDrawer method does nothing.
By using
mDrawerLayout.isDrawerOpen(mFragmentContainerView);
before and after calling the openDrawer method, I can see it is always closed.
I have spent several hours on this. What is going on? Any clue is appreciated!
Open and close the drawer using it's gravity like this
if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
mDrawerLayout.closeDrawers();
} else {
mDrawerLayout.openDrawer(Gravity.START);
}
or use View.VISIBLE and View.GONE in place of Gravity.
I'm implementing an Android application with a Navigation Drawer and I have a problem with it.
I started from the template (Android app with navigation drawer) in Android studio and added a new section to it - Settings. When clicking the settings button the user is taken to a new Activity and the user can go BACK either by pressing the "up" button in the action bar (Which works perfectly) or by pressing the physical back button. That part doesn't work so good.
When pressing the "up" button and then opening the navigation bar, my home page is highlighted (selected). But when using the back button and opening the navigation drawer, the Settings item is selected, as if the user is viewing that page and not the home page.
So this is because the Navigation Drawer fragment isn't updated and I havent found any way to solve this problem. What I would like is for the Navigation Drawer to be recreated (Which is what I think happens when pressing the "up" button). Do you know how I can make this happen?
I started working on a solution based on this:
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
// Update your UI here.
}
});
But I don't know what to write there that will update the whole UI. (experimented with calling onCreate() but it's too ugly and can't be the right way).
Any suggestions?
UPDATE:
The onBackStackChanged event isn't sent when pressing BACK from another activity. Only for fragments in the current actvity.
You have to override the actionbar back button like this :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
//or
super.onBackPressed();
break;
}
return true;
}
I had the same problem.I used notifyDataSetChanged() in the ListView Adapter.
In getView() i used,
if(mListView.isItemChecked(position)) {
convertView.setBackgroundColor(getResources().getColor(R.color.md_black_1000_12));
}else{
convertView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
notifyDataSetChanged();
I implemented my left slide drawer for menu options using Simple-side-drawer Library. Its working fine but the issue is, the drawer layout menu options are responding to onclick events even after my drawer is closed state ie (menu options responding to onclick events from my MainActivity).
You can check SimpleSideDrawer's isClosed() method to make sure if it is closed.
SimpleSideDrawer menuDrawerLeft = new SimpleSideDrawer( this );
menuDrawerLeft.setLeftBehindContentView(R.layout.menu_drawer_left);
final Button buttonInLeftDrawer = (Button)findViewById(R.id.buttonInLeftDrawer);
buttonInLeftDrawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (menuDrawerLeft.isClosed()) {return;}
//runs only when the drawer is open
}
});
Without looking at your code it's hard to give any suggestions.
One advice though. Are you using this library:
https://github.com/adamrocker/simple-side-drawer
If so, please consider using the built-in Navigation Drawer that Google has released instead. It follows the design guidelines (which Simple Side Drawer does not) and is well tested to work.
See these resources for your implementation:
http://developer.android.com/design/patterns/navigation-drawer.html
http://developer.android.com/training/implementing-navigation/nav-drawer.html
I have an Activity class that have a DrawerLayout. That layout shows a list in Drawer and let user to switch between fragments. In these Fragments, there are some URLs and when user clicsk on that, a WebviewFragment would be shown. However, I don't want to show the DrawerLayout in the WebViewFragment. Instead, I would prefer user would being redirected to previous Fragment.
Is there any way for me to show/hide the DrawerLayout depends on what the current Fragment is?
I try to call mDrawerLayout.setVisibility(View.GONE), but it seems that it is not complete. At least the ActionBar icon is still the drawer icon.
You can use this method to lock or unlock the drawer: DrawerLayout.setDrawerLockMode(...). (There are also two other versions of this method to specify a lock mode for specific drawers.) To lock, use DrawerLayout.LOCK_MODE_LOCKED_CLOSED; to unlock, use DrawerLayout.LOCK_MODE_UNLOCKED.
If you are using the ActionBarDrawerToggle, you need to add some extra code to prevent the drawer from opening when they click the ActionBarDrawerToggle if you've locked the drawer.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// check lock mode before passing to ActionBarDrawerToggle
// I assume your drawer is on the left; if not, use Gravity.RIGHT
int lockMode = mDrawer.getDrawerLockMode(Gravity.LEFT);
if (lockMode == DrawerLayout.LOCK_MODE_UNLOCKED &&
mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
Why not open a new Activity that has only a WebView as its content instead? Then the user will have to press the back button to go back to the Activity with the DrawerLayout, and then there will be no problems.
Alternatively, you don't have to have such an activity yourself, you can let Android find a browser for them to open instead using this code:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("HTTP_URL_GOES_HERE"));
startActivity(intent);
I am using a Navigation Drawer in my Android app.
As this is a pattern that I find really hard to discover, my plan was to add a little message at the bottom of the screen until the user finally discover it and succesfully opened it with a swipe.
I know that I could use:
public void onDrawerOpened(View drawerView) {
// Stop telling the user, he know how it works
}
But this action is also triggered when opening it with the upper left button in the ActionBar.
Any suggestion to detect a succesfull swipe would be warmly welcome.
You need to listen to the state change callback:
#Override
public void onDrawerStateChanged(int newState) {
if (newState == DrawerLayout.STATE_DRAGGING) {
Log.v(TAG, "Drawer opened by dragging");
}
}
This state only occurs when user drags the drawer into view. Tapping on home icon does not induce this - which is exactly what you want.
Without extending DrawerLayout you could use a combination of the DrawerListener and a boolean flag for Home button press.
Implement DrawerListener with onDrawerSlide method. Inside this method you can check the Home Button flag and if this is false and the drawer is sliding open then you know this was action through the slide.
Bit messy.
Other than that override DrawerLayout and override the onTouchEvent and catch move events.