I am trying to close my navigation drawer after I finish certain activities immediately, without the sliding animation. I don't close it on activity start because I would like it to stay open if the user backs out of the activity. However, I cannot get it to close without a brief flickering animation as it closes itself. I have tried variations of the following code:
protected void closeDrawerImmediate() {
mDrawerLayout.setVisibility(View.GONE);
mDrawerLayout.closeDrawers();
mDrawerLayout.setX(0);
mDrawerLayout.setVisibility(View.VISIBLE);
}
You can use the new DrawerLayout.closeDrawer(int/View, bool) methods in v24 of the support library to instantly close a drawer:
drawerLayout.closeDrawer(Gravity.LEFT, false);
If you just want to close a drawer immediately, such as when you're launching an activity from tapping on a drawer item, that is probably enough.
If you want to close the drawer on coming back to the activity, I'd set a stateful boolean like closeDrawerOnResume = true wherever is appropriate (eg. starting a new activity), and then in onResume check this boolean and close the drawer without animating if it is true.
You can consider setting animation time to 0. But it seems it's not as easy as setting one property. Here is tutorial and the code but it requires importing sources for DrawerLayout and ViewDragHelper into project and small modifications there.
Related
I'm working on an app that only uses a single Activity and switches out fragments as they are needed with the navigation drawer.
Now we want to navigate back from one of these fragments by using the homeAsUp button in the ActionBar.
I have followed all the steps to set the button up. From disabling the navigation drawer setDrawerIndicatorEnabled(false) and calling setDisplayHomeAsUpEnabled(true) in the fragment's onCreateView().
I also set setHomeButtonEnabled(true) in the MainActivity's onCreate() however because the app is already in the MainActivity, we cannot specify a Parent Activity.
Whenever I run a fresh install of the app, the homeAsUp button works and is registered in the onBackPressed(), not onOptionsItemSelected() method. However, when I close the app and run it again the button does not even register clicks.
In onBackPressed() I check a few conditions, but it does not block the button press.
In onOptionsItemSelected() I check for android.R.id.home.
Unfortunately, I cannot post the code.
This post describes what I'm trying to achieve: Switching between Android Navigation Drawer image and Up caret when using fragments
I managed to fix the issue I was experiencing. It was a very simple mistake.
Because I'm not the original author of this code, I went through the MainActivity thoroughly.
As it turns out the original author called setDisplayHomeAsUpEnabled in the onCreate function (which is extremely long), but near the end he also called setSupportActionBar, which made the first call of setDisplayHomeAsUpEnabled useless. Moving setDisplayHomeAsUpEnabled below setSupportActionBar fixed my problem.
If you did everything correctly, make sure that your code is written in the correct order.
Furthermore, if you use custom toolbars in other fragments, remember to set your original Support Action Bar by calling setSupportActionBar(toolbar.find(this)) in onResume of your MainActivity.
I have navigation drawer which using mostly fragments for loading views but one activity which confuse me.you can see my drawer below and assume that i have to start activity when clicking the option Profile.if i starting the activity do i need to return to home screen or i need to return to the fragment from where the activity launched ?
Problem 2 : Am replacing the selected drawer icon as you can see in the attachment below.when clicking the Profile option do i need to replace the Profile option icon as selected which is not feeling comfortable for me.?
And the genuine Problem : Drawer look like stucked or not properly ie , not smoothly closing when calling startActivity.
And one more : What about finishing navigation activity when launching new activity and starting freshly after finish the new one ?
Answer for problem 1: It is depend on you where you are writing a code i. e. click event for drawer layout. if you are using fragment then write a code in fragment and fragment is a part of activity so obviously you will going to use getActivity().startActivity(intent);
Answer for problem 2: This is not mandatory that use have to replace a icon. you can use tint functionality to change the icon color or you can use selector or use can use ripple which is default in android 5.0 and above.
Answer for problem 3: Sorry i am facing also some problem so will update on this soon.
Answer for problem 4: Why you are finishing navigation activity. Ultimately you will come back to navigation activity so don't finish navigation activity. Just start new activity. once new activity finish it will come back to navigation activity.
Hope this will help for you.
I am using android slider.jar for animation. while opening left/right side menu using this mNav.toggleLeftDrawer(); and mNav.toggleRightDrawer(); with help of slide_left_in and slide_left_out , slide_right_in and slide_right_out for transition xml animations..
here animation are working fine.
my problem. when i click menu from left or right side, drawer is opening, when i click items from left or right side, its going to start a new activity like this both activities items.
while clicking item from left side menu that time, first time new activity are opening. that time, my previous animated drawer is opened state. its does not hiding.
before starting new activity, the previous drawer is closing first, and then only new activity is starting.
I want directly go to the new activity without showing previous animated state..
I have my sample code link
http://www.mediafire.com/download/475tpnob648u6vc/SimpleSideDrawerExample.zip
I created a NavigationDrawerFragment, which is actually just a slightly modified version of the code Android Studio generates.
I have a Dashboard activity that contains NavigationDrawerFragment and that can launch several other activities, each of which also contain the NavigationDrawerFragment. Let's call one of these activities Foo.
If I enter Foo from the Dashboard using the Dashboard's nav drawer, then open Foo's nav drawer, the back button will correctly close the nav drawer. HOWEVER, if I entered Foo using a UI element (that was in place before I implemented a nav drawer), then open Foo's nav drawer, the back button returns to the Dashboard activity rather than closing the nav drawer.
I can't figure out why this is occuring. For the NavigationDrawerFragment I have an ItemClickListener and for the UI element I have a ClickListener, but in both situations I am launching a new Foo activity using an intent.
I am wondering if there is something being set that I just don't realize, and if I have control over that something.
I know that I could override onBackPressed to check for the drawer being open (as shown here), but that feels hacky as there is obviously some behavior happening here that I am not aware of.
Edit: This has something to do with ActionBar-PullToRefresh. I removed it from Foo and the nav drawer works correctly.
Edit 2: I was wrong about pull-to-refresh, as #adneal pointed out. There must have been a coincidence that made me think that. I ended up overriding onBackPressed and conditionally checking if the drawer is open or not. Also as #adneal pointed out, the problem has to do with focus. I am still so very curious as to how the focus could be different when launching the activities the same way, but from different places.
So, I'm fairly certain I understand what's going on, but I'm still unsure about why it only seem to happen when you use a View.OnClicklistener. Maybe there's a little more to your code that I don't know about or maybe I overlooked something in those regards.
But the reason you're having this problem is due to how Android handles the View.onKey... event. DrawerLayout overrides View.onKeyDown and View.onKeyUp, checking each time if you're pressing the "back" button so it can determine whether or not to close the drawer. But this event isn't taking place, in your case, because the AdapterView you have set will take the focus away from the DrawerLayout after you call AdapterView.setAdpater.
How to fix it
Short of copying over the DrawerLayout source and modifying it, which you could definitely do; it's only one class, I would recommend converting each Foo Activity into a Fragment, then use the FrameLayout you typically place when creating a DrawerLayout to display them. You should create a NavigationDrawerActivity instead of a Fragment to host everything.
This has to do with the way DrawerLayout is laid out, in that it has to use MeasureSpec.EXACTLY. In other words, when you place your NavigationDrawerFragment ontop of your Foo layout, it takes up the entire space. If you called View.setFocusableInTouchMode after you set your ListAdapter in Foo, the DrawerLayout would work like it should, but the AdapterView wouldn't scroll anymore. And you can't toggle that focus based on the open or closed position of the DrawerLayout, because either way it takes up the entire space and always return true in View.onTouchEvent.
Or just override Activity.onBackPressed, which really isn't as "hacky" as you may think, but I understand what you mean.
At any rate, I hope this is clear and/or helpful at the very least. Took me a little bit to reproduce your problem, but after I did it didn't take long to rule out the pull-to-refresh library. In short, I think it's a focus issue. Prove me wrong.
Source
You can view the source for the DrawerLayout here to look over how it handles the "back" event
You can see here how AdapterView.checkFocus works
You can see here in ListView (GridView is the same), when AdapterView.checkFocus is called
I have a NavigationDrawer having a list with many radio buttons.I want the user to have to select a list item before he can close the Drawer.
The drawer should not close till the item is selected.
Is there a clean way with which I can achieve this??
Set up the DrawerLayout lock mode setDrawerLockMode as follow:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
but you need to handle actionbar home up button.
I would recommend not restricting drawer operations, and just set a default selection, in case the user doesn't select an option.
However, if you insist on doing it, this method seems to be the one to go with:
// Belonging to DrawerLayout
public void setDrawerLockMode (int lockMode, View drawerView)
In onCreate() open it and lock it, then unlock it upon the selection of any of the options. There is no need to recheck during the usage, as radio implies there is always a selection, unlike check box selection.
That would be my first attempt at this, let me know, cheers.
Reference