Replacing inflateMenu at Runtime - android

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

Related

<layout_gravity="start"> doesn't work on NavigationView?

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>

Android NavigationView set menu from XML programmatically

How do I create Android Navigation View menu from XML programmatically?
Using design support library you can add Navigation view easily.
Add below code in xml:
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:headerLayout="#layout/layout_drawer_header"
app:itemTextColor="#000000"
app:menu="#menu/global"></android.support.design.widget.NavigationView>
and then create a menu file for navigation

Navigation Drawer Menu Scroll Effect

I'm using NavigationView in DrawerLayout for my app's navigation menu here's the xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<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"
android:fitsSystemWindows="true"
android:foreground="?android:attr/selectableItemBackground"
android:theme="#style/NavigationDrawerStyle"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#color/textColor"
app:itemTextColor="#color/textColor"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
The problem: I don't even have enough items to scroll but I'm seeing this effect while trying to scroll, I want to disable this effect, thank you.
Scrolling effect
Update
trying both:
android:overScrollMode "never" in NavigationView
and
navigationView.setVerticalFadingEdgeEnabled(false);
didn't work.
I'm calling following method in onCreate() :
public void setupDrawerLayout() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolBar, R.string.drawer_open, R.string.drawer_close);
drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
You are trying to disable the scrolling shadow.
Try adding this to your NavigationView:
android:overScrollMode="never"
If the above solution doesn't work, try adding this in the onCreate() method of your Activity or wherever you setup your UI:
mNavigationView.setVerticalFadingEdgeEnabled(false);
UPDATE
After some more testing I found that indeed, the solutions posted in the answers so far are not working. However, I found this to work:
for (int i = 0; i < navigationView.getChildCount(); i++) {
navigationView.getChildAt(i).setOverScrollMode(View.OVER_SCROLL_NEVER);
}
Add it in your onCreate() method, after you initialise the navigation drawer.

remove or hide header navigationView in android

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"

NavigationView how to handle dynamic header content

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);

Categories

Resources