Navigation Components: Switch Menu of Navigation Drawer (for logged in users) - java

I am using the new Navigation Components library. I have a Navigation Drawer in place that works perfectly fine.
If my user logs in I want to change the menu of the Navigation Drawer. Specifically, I want to change the "Login" item to "Logout" from within a fragment.
All the solutions I found online didn't use Navigation Components. So therefore, they recommended something like this:
// Get the navigationView and swap the menu
NavigationView navigationView = view.findViewById(R.id.nav_view);
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer_logged_in);
However, this doesn't seem to work in my case (I cannot get the navigationView from within a fragment). How does one achieve this with Navigation Components?

You can access Activity from Fragment through interface:
Create and interface say LoginSuccessListener
Implements this in your Activity
Inside fragment receive this listener inside onAttach
Use it as you needed to update Navigation Menu

Related

How can i set different onBackPressed() for different fragment of a NavigationDrawer Activity?

I I need to change the toolbar icon of my main_activity(Navigation Drawer Activity) and replace it with back icon and with a different function(go back).
How can i change different toolbar with different function for all my fragment?
I'd suggest starting a new activity instead.
This way you don't have to modify the drawer icon and touch handling of the icon is also separated. (You would just close your new activity when the up navigation is clicked).
However, if you want to do it all in your current Activity, you need to change the ActionBar / Toolbar icon and override the clickListener of the navigation icon.
toolbar.setNavigationIcon(R.drawable.ic_arrow_back);
and
toolbar.setNavigationOnClickListener(yourClickListener);

Android Dev: Navigation View vs App Bar

I'm new to Android development. I get that a NavigationView is an AppBar (i think). What makes these two different? I want to create a custom "appbar/toolbar" with a centered logo and menu/settings button in the top right corner (instead of the standard left) which reveals a drawer.
I was going to ditch the built in appbar/toolbar all together and just create my own somehow and include button overlay which displays a drawer.
What would you do? Navigation view, app bar, or custom toolbar from scratch? I don't know what the standard is or what is acceptable. What is the difference between a navigationview and appbar. Thank you.
edit: I'm slowing realizing that an appbar is one feature within a navigation view, among others like a drawer layout, menu items etc... i think.
1. NavigationView
By using NavigationView, we can bind the menu directly with NavigationView. This is the benefit of the NavigationView. No need to create ListView and adapter with navigation drawer. By default we can get selector of item click. With menu we can change the color of icon of selected menu.
For more details :
1.https://developer.android.com/reference/android/support/design/widget/NavigationView.html
http://www.technotalkative.com/part-4-playing-with-navigationview/
2. AppBar
Appbar is for toolbar with scrolling effect. We can easily give the material design effect.
For more details :
1.https://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

Side bar in android

Does anyone know what this UI element is called in Android, and where to look for the documentation to implement such a layout.
Yup its called a navigation drawer, here is a link to some documentation https://developer.android.com/training/implementing-navigation/nav-drawer.html. Also I believe its one of the standard options when creating an initial project
Navigation Drawer Android Example as follow:
http://javatechig.com/android/navigation-drawer-android-example
its called navigation drawer in android.
to add navigation drawer simply follow these steps-
in android studio go to file - new project - your application name click on next - next - to add an activity scroll down in pop up window - and select navigation drawer activity click to next - type activity name as required - finish.
it will add navigation drawer in your project.
This type of user interface is the most common User Interface used in modern android apps. It is called a Navigation Drawer. The drawer stays outside the screen on the left/right side(where you choose it to be) and comes to action when you click the open drawer button.
For development reference, you can head over to the documentation page.
If you want to add a new such activity to your app, you can click New>activity>Navigation Drawer Activity.
Or when starting a new project, on the Add an Activity to Mobile Screen Page, you can select a Navigation Drawer Activity.

How to disable navigation drawer of activity from within a fragment

I've been looking for an answer to this for a while but I haven't found any answers on the subject.
So I have my Main Activity which containers a toolbar, a nav drawer, and a frame layout (the container for my fragments). So on the start of the activity it starts a fragment to display and I can switch between different fragments and they all have the toolbar and nav drawer because their container is still main activity.
Except there is one problem. I want to make a new fragment and I don't want it to use a nav drawer or a toolbar. I figured out how to disable and hide the toolbar from within that specific fragment but I haven't been able to find out how to disable the nav drawer. The user can still swipe from left to right to open the nav drawer. I don't want them to be able to do that.
Also if I was going to disable it, I need to disable it from the fragment not the main activity because the navigation to this fragment is from a button that's inside another fragment
You can use :
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
This will lock drawer opening on swipe
Source : How to Disable drawer in fragment and back back to correct fragment

Navigation Drawer app behaivour when open fragments

Im working with a Android app that uses Navigation Drawer. This app use the template provided with Eclipse ADT (when you select the navigation drawer template).
I dont understand the behaivour of the navigation drawer very well.
My main requeriment is make a "search" option, I have on the Navigation Drawer a EditText to get the query string from the user. I want that I press search button and open my SearchFragment getting the search query.
I know that I can make this:
Bundle args = new Bundle();
args.putString("searchQuery",searchQuery);
fragment.setArguments(args);
And this for getting:
getArguments().getString("searchQuery");
But I have the next:
MainActivity
NavigationDrawerFragment
SearchFragment
OtherFragments.. (I think this arent relevant in the question)
I dont understand where I can make this steps.
More data:
I have in the navigation drawer class the EditText with the setOnEditorActionListener for the search press.
Any info that I can add I will be add. Sorry my english
Thanks for the help
Navigation drawer is basically a component that show 2 pane in the way that one is the main pane (usually fragment) that is used to show content and the other one is usually a listview that is just used to choose which fragment to show in main pane
so if you want to switch between 2 different layouts you have to create 2 different fragments and then you can add or replace these fragments based on listview's selected item.
You may refer to below link for complete code
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Hope it helps!

Categories

Resources