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.
Related
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
I created a new App with Android Studio 2.0 and I want to have my Navigation Drawer below the Toolbar. I know the Material Design specs say it should be above the Toolbar but my Drawer won't have that much entries and I want to see the neat icon animation provided by Google.
I maged to get what I want by placing the DrawerLayout within the top level CoordinatorLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.company.app.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:openDrawer="start">
<include layout="#layout/content_main" />
<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:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>
I am using android:fitsSystemWindow="true" on the top level layout so I can tint the status bar. But when I do that, the Navigation Drawer does have a gray top border.
When I would be using it the way Google specified it (above the Toolbar) then the border would be fine but this way it just looks ugly. Is there any way to remove this?
I experimented with overriding NavigationView.onInsetsChanged(Rect rect) and was able to place the items at the top but the gray border remained.
I had a headache trying to figure this out also and managed to find this which describes how CoordinatorLayout passes fitsSystemWindows to its child. I definitely think this is a bug since NavigationView already has it set to false, but then I read this:
NavigationView takes care of the scrim protection of the status bar for you, ensuring that your NavigationView interacts with the status bar appropriately on API21+ devices.
from the Android Developers Blog. So I'm not sure what to think of it. I think it might be due to the fact that NavigationView was never intended to be used this way.
Nonetheless, here's a solution you can try that fixes the gray bar issue, but I haven't figured out if it might cause anymore problems. If so please post back.
In the activity_main.xml:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:fitsSystemWindows="false"/>
</android.support.design.widget.CoordinatorLayout>
In MainActivity class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
navigationView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
#Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
return insets;
}
});
}
}
}
Should get you this:
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);
See here what i mean
Hi, I'm using stock Navigation drawer v4 and i ask how can delete that background shadow when navigation drawer is open.
this is my code of NavigationDrawerFragment.java
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.trasparent, GravityCompat.START);
// set up the drawer's list view with items and click listener
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
And this is MainActivity.xml
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Main layout -->
<FrameLayout
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/ImageView3_Left"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:background="#FFFFFFFF"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"/>
</android.support.v4.widget.DrawerLayout>
I find the solution :D
mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
Adding this in MainActivity.java
This is works for me in androidX
/*Navigation drawer with transparent background*/
mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
/*Remove navigation drawer shadow/fadding*/
mDrawerLayout.setDrawerElevation(0);
You can use the setDrawerShadow method to point to a transparent drawable:
navigationDrawer.setDrawerShadow(R.drawable.someDrawable, GravityCompat.START);
Has anyone successfully got the navigation drawer to open over top of a toolbar that is being used with setSupportActionBar(toolbar)? I am using Theme.AppCompat.Light.NoActionBar.
If I make the Toolbar a child of the Drawerlayout in my xml, then the toolbar fills the entire screen no matter what height I set it to. The only other thing I could think of doing was wrapping my drawerlayout and toolbar in another parent layout and placing the toolbar outside of the navdrawer, but then the navdrawer opens up below the toolbar.
I was also not successful in getting the hamburger icon to transition to an arrow when using the navdrawer. I am using the new ActionBarDrawerToggle from the v7 compat library as well.
So I created a new test project targetting api21 only and used the following XML, and it also has the same problem where the toolbar fills the entire screen. The navdrawer DOES open over top of it in with this code.
Any help is greatly appreciated. Thanks
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.example.testnav.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer"/>
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="#43599a"/>
</android.support.v4.widget.DrawerLayout>
Here is my toolbar code from my activity:
final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
if (mActionBarToolbar != null) {
setActionBar(mActionBarToolbar);
}
if (mActionBarToolbar != null) {
mActionBarToolbar.setNavigationIcon(R.drawable.ic_drawer);
mActionBarToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
mDrawerLayout.closeDrawer(Gravity.START);
} else {
mDrawerLayout.openDrawer(Gravity.START);
}
}
});
}
It can be a solution.
<!-- Main layout -->
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/headerbar"
android:orientation="vertical">
<!--Toolbar-->
<include layout="#layout/toolbar"/>
<include layout="#layout/main_container" />
</LinearLayout>
<!-- Nav drawer -->
<include layout="#layout/navdrawer" />
Another way is to put the toolbar inside the layout which you use in your main container.