I have a pretty standard NavigationView. When i use a static layout in the header like below it works perfect.
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_view"/>
But i want to have a dynamic header so thah i can change it when user logged in etc... So i tried to use a fragment instead of nav_header.xml
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/fragment_header"
app:menu="#menu/drawer_view"/>
Can i use a fragment in the headerLayout so i can handle all my logic in the fragment's java file. Or what is the right solution to handle this problem.
You can do that in code by inflating custom layout and set it's header for navigation view.
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
View nav_header = LayoutInflater.from(this).inflate(R.layout.nav_header, null);
((TextView) nav_header.findViewById(R.id.name)).setText("UserName");
navigationView.addHeaderView(nav_header);
You don't need to set app:headerLayout in xml.
You could call the header declared on xml like this:
NavigationView navigationView= (NavigationView) findViewById (R.id.navigationView);
View header = navigationView.getHeaderView(0);
And then get the views like this:
TextView text = (TextView) header.findViewById(R.id.text);
Related
Currently I have three options in my bottom navigation and a navigation graph for them.
part of my mainActivity.xml file look like this:
<fragment
android:id = "#+id/nav_host_fragment"
android:layout_width = "match_parent"
android:layout_height = "0dp"
android:layout_weight = "1"
android:name = "androidx.navigation.fragment.NavHostFragment"
app:navGraph = "#navigation/nav_graph"
app:defaultNavHost = "true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id = "#+id/bottom_nav"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
app:menu = "#menu/bottom_nav" />
and In my mainActivity I have written this code,
navController = Navigation.findNavController(this, R.id.nav_host_fragment)
bottom_nav.setupWithNavController(navController)
NavigationUI.setupActionBarWithNavController(this, navController)
everything related to navigation is handled by jetpack navigation library.
Now I want to add a Navigation drawer also and in drawer I want to add different menu items(not only three that are in bottom navigation), so I will add new menu resource file for navigation drawer, now how should I use navigation library for both bottom nav and nav drawer? I don't want to manually do Fragment transactions and work with fragment manager.
One approach that I can think of is adding all of the fragments in a single nav graph(which is currently used for bottom nav) and then use the same navController for nav drawer also but I am looking for a better approach.
The structure of drawer is you have to include activity INSIDE drawer layout. So in your case the implementation of the bottom navigation is you include bottom_navigation INSIDE main_activity ---> main_activity INSIDE drawer_layout
Code sample
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
SRC https://code.tutsplus.com/tutorials/how-to-code-a-navigation-drawer-in-an-android-app--cms-30263
I have method t change the menu of navigation drawer, I remove the line at XML file activity_main.xml
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
/>
so I can add the menu resource from code
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.inflateMenu(R.menu.activity_main_drawer);
navigationView.setNavigationItemSelectedListener(this);
But when I need to replace the menu by another resource the menu will be appended to the first one, but I need to remove the oldest
plz help me
Thanks for #Mohsen
navigationView.getMenu().clear() it will do it
hi i have a navigation view with a drawer layout i want to looks like but mine is look like
and this is my code
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<include layout="#layout/activity_multi_tab"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="60dp"
app:headerLayout="#layout/header"
app:menu="#menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
Shift your navigation view inside the DrawerLayout but below the included layout.
Navigation view has to be the second child of DrawerLayout
Also change the layout gravity to start.
android:layout_gravity="start"
And remove the margin top. There'll otherwise be a space at the top
Refer this official documentation for more details on the view
https://developer.android.com/reference/android/support/design/widget/NavigationView.html
I'm trying to make a Navigation Drawer, and inside the NavigationView I've added the layout_gravity="start" attribute, but it wont hide the View. this is my code:
<RelativeLayout ... >
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_menu"></android.support.design.widget.NavigationView>
</RelativeLayout>
What could be wrong? Thanks.
If you intend for the NavigationView to behave as a drawer, then the root View should be <android.support.v4.widget.DrawerLayout>, instead of <RelativeLayout>
I hope this works for you because layout_gravity cannot be used inside Relativelayout.
<androidx.drawerlayout.widget.DrawerLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_menu"></android.support.design.widget.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
i am using design 23.1.1 in my project. i want drawer without header. just menu items are enough. i dont add any header view(programmatically or in XML). but in drawer i have empty header. please help me how to remove this empty header.
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:menu="#menu/drawer"
/>
View headerView= LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
navigationView.addHeaderView(headerView);
navigationView.getHeaderView(0).setVisibility(View.GONE);
i use dummy layout. and solved my problem but i think it is ridiculous.
If You want to remove header:
navigationView.removeHeaderView(navigationView.getHeaderView(0));
Of cource if You have more haeders You have to do this in some loop to remove them all. 0 is index of the first one.
try to add this to your style file <item name="android:windowFullscreen">true</item>
and remove app:headerLayout from navigationView xml.
and add this to your navigationView xml :
android:layout_marginTop ="#dimen/abc_action_bar_default_height_material"
it worked perfect for me.
To remove it permanently:
The Layout that contains the NavigationView widget is like this:
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
Just remove the app:headerLayout line from it:
app:headerLayout="#layout/nav_header_main"