How to disable drawer option in specific fragments of an activity - android

I have an activity with 5 fragments, I used drawer layout in my activity for a drawer, but I want to use(enable) the drawer in only fragment 2 and I want to disable the drawer option in remaining fragments.
Can any one help me how to do this?

Put two methods in the your activity, one to disable the drawer and one to enable it again, like so:
public void lockDrawer() {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
public void unlockDrawer() {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
Then in your fragments onCreateView(...) method put:
fragmentInteractionListener.lockDrawer();
for the fragments where the drawer should stay shut and for the fragments where the drawer should stay open put:
fragmentInteractionListener.unlockDrawer();
P.S: for tutorials in how to correctly implement a fragment interaction listener see:
https://developer.android.com/training/basics/fragments/communicating.html

Related

How to set toolbar.setNavigationOnClickListener only for current fragment?

Started new project from navigation drawer activity template. By default, all drawer menu items are at the same navigation level. All fragments display hamburger menu button, that shows drawer.
I need to keep all items in drawer, but place Home as top level item and others as it's children like this:
Tried to overwrite toolbar?.setNavigationOnClickListener { } in Gallery fragment, but it also affects Home fragment and I didn't find how to restore default behavior.
How can I set Home as navigation parent for others or how to set navigation click listener to only one fragment?
For example you have set tag for every fragment to trace when you start new fragment and now check with Home and set click event..
Fragment home = getSupportFragmentManager().findFragmentByTag("home");

Create a Navigation drawer to link to fragments and activities

So i have a navigation drawer like so it has 3 buttons that go to 2 different fragments and the other button which goes to an Activity.
When I click one frag 1 my fragment opens up with the drawer still intact same goes for Frag 2
but when i click on the Activity 1 the Drawer disappears
but i would like the drawer to continue in the activity as well.
can this be done.
What you'd want to do (roughly) is
Establish a menu as one of your resources and establish the items in that list.
In the activity that you want to contain the drawer, create
the drawer object and create a callback for the onMenuItemSelected.
In that callback, reference the menu item ids you created before and use intents and fragment managers to either start the activity or fragment that you want based on what they select.
This can't be done.
The DrawerLayout lives within your Activity and the Fragments that you switch to also live within the same Activity. This is the reason why switching fragments will leave the drawer intact. It's because they both exist within the same Activity without any interference.
However, launching an Activity is different. This is a completely different Activity which has it's own layout.
You actually only have two options if you wish to continue using a Drawer for main navigation.
Remove the need for the second Activity and change that to a Fragment. This way, all your fragments will exist within the same parent Activity so it'll use the same drawer that exists in that parent Activity.
Create an identical DrawerLayout and NavigationView in the second Activity. Call code to have the Drawer be opened when it's created. This way, although you're not really using the same Drawer, you're giving the illusion that it's still the same Drawer.

Change Navigation Drawer selected item from within Fragment

I have a navigation drawer in my main activity, and 2 fragments (A and B). The Navigation has two items (A and B). When the application is started the main activity shows and displays fragment A in the FrameLayout.
Fragment A contains a button that replaces the FrameLayout content to Fragment B. The .replace works fine, but I'm not sure how to change the selected drawer item from A to B.
Jayesh helped me with this... didn't realize it was easy. For those interested in my main activity (which hosts the navigation drawer):
public static void changeDrawerItem(int Position) {
mDrawerList.setItemChecked(1, true);
}
Note: mDrawerList is my Drawer List. Not sure if this is the best way to change the item, but it'll allow me to change the drawer item from other fragments I create.
In the fragment I simply call the method with
MainActivity.changeDrawerItem(1);

While using a navgiation drawer and a viewpager: how to swipe another full screen fragment over/instead of the others

The App uses a Navigation Drawer and a Viewpager (with each tab contains a fragment).
From the actionbar I would like to show an extra full screen fragment instead of the other fragments. This fragment could contain e.g. the user settings.
And ...
It would be very nice to use the top-left "<" back button of the navigation drawer to leave the new fragment and show the viewpager with the tabs again.
The most beautiful option would be the new fragment pushing the other fragments aside.
I don't want to start a new Activity, as I am working with pleasure with Fragements in all my Apps.
The DialogFragment is not suitable (as I tried).
First thing you need to do is replace the fragment:
getFragmentManager()
.beginTransaction()
.replace(R.id.container, new Fragment())
.addToBackStack("TAG")
.commit();
You can use one of the various methods available to animate this, see http://developer.android.com/reference/android/app/FragmentTransaction.html
Disable the nav drawer and you should get a back button, if not you can call (I think you should have already done this to setup the nav drawer):
getActionBar().setDisplayHomeAsUpEnabled(true);
You'll need to handle the back button press by implementing onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Go back
break;
}
}
You'll probably need to handle the hardware back button in your activity too.

Navigation drawer with fragments and only one activity

Im doing an app with navigation drawer.
For this, i have a HomeActivity, this contains all the login of my navigation drawer, the options in menu, the view, the titles etc. And here, i set listenerclick for navigation elements.
This listener receives FragmentManager and with a switch do:
smf.beginTransaction().add(R.id.frame_content, new Fragment()).commit();
Replacing fragment for the fragment that i need in each case of switch.
In home layout i have a framelayout and navigation drawer.
Mi question is, is correct that i only have one activity with a framelayout, and depends on the item clicked in navigation drawer i replace the fragment on the frame, or is better have lot of activitys, and create menu in all of them with the same login, and when user click in item menu, launch new intent with the activity selected?
I hope i have explained ok...
Thank you.
I did this same thing, but I found it was a lot better to have different activities.
If you do go down the separate activities path you should have one base activity that the activities extend so you don't need to rewrite the drawer code.
A fragment is only really meant to be an extension of an activity, for example when you have multiple tabs, or you are swiping between different views, or you need to break up your activity into sections.

Categories

Resources