Android hide NavigationView header - android

I am working on an android app and it requires DrawerLayout below the ToolBar. I've achieved that but NavigationView's header i.e Status Bar 24dp is visible even after removing the header.
Is there a way to remove that?
XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:orientation="vertical"
>
<include
layout="#layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v4.widget.DrawerLayout
android:layout_marginTop="?android:attr/actionBarSize"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.NavigationView
android:paddingBottom="0dp"
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#null"
app:menu="#menu/activity_home_drawer"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

try this one :
View headerView = navigationView.inflateHeaderView(R.layout.nav_header_home);
headerView.setVisibility(View.GONE);
and remove the line app:headerLayout="#null" from your xml.

Also try like this,
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:background="#color/list_background"/>

Probably because the height is not enough. Why don't you use wrap_content with layout="#layout/app_bar_home" and add android:layout_below to your DrawerLayout?
Or to keep it simple, you can also use LinearLayout in this case. It would be easier:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
>
<include
layout="#layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.NavigationView
android:paddingBottom="0dp"
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#null"
app:menu="#menu/activity_home_drawer"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>

Remove app:headerLayout="#null" from your navigationview.
inflate your headerlayout from onCreate() like this, and then set the visiblity gone.
View headerView= LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
navigationView.addHeaderView(headerView);
navigationView.getHeaderView(0).setVisibility(View.GONE);

Define groups in menu and then hide group using:
navigationView.getMenu().setGroupVisible(groupPosition, false);
Also, remove headerLayout attribue.

Related

ListView not scrolling in nav_header

I'm trying to have a listView in my navigation drawer header. I have the list there and it populates fine, the only issue is that I can't scroll the list. It seems the nav drawer itself is intercepting the scroll action and not allowing me to scroll the ListView
nav_header_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="260dp"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical">
<ListView
android:id="#+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
and the drawer
<?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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_drawer"
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"
app:headerLayout="#layout/nav_header_drawer"
app:menu="#menu/activity_drawer_drawer" />
If you need any more files, I can post
You can customize navigation drawer as you want. design a separate layout say nav_layout.xml and in your activity_home.xml replace your navigation view with below code.
<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">
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/nav_layout" />
</android.support.design.widget.NavigationView>
Now open your nav_layout.xml and put below code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="260dp"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical">
<ListView
android:id="#+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
in this way you can customize navigation drawer as you want.
For the listview xml try adding one or both:
android:focusable="true"
android:focusableInTouchMode="true"
I had to do this for buttons created in a listview which were not responding to touch events.

How to use weight for android:layout_marginLeft attribute android

I have the below code for navigation drawer and i am using the attribute
android:layout_marginLeft="620dp" to specify how much space(width) the drawer will occupy when the drawer layout is opened.
instead of hardcoding fixed width 620dp i want to specify like weight or is there any similar approach like weight for this attribute??
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/header"
android:layout_marginLeft="620dp"
android:fitsSystemWindows="true"
app:itemBackground="#android:color/transparent"
>
<ExpandableListView
android:id="#+id/navigationmenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="130dp"
android:divider="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:dividerHeight="0dp"
android:fitsSystemWindows="true"
android:background="#android:color/white">
</ExpandableListView>
</android.support.design.widget.NavigationView>
In the respective Activity/Fragment of the layout you can find the NavigationView by id:
dummyNavigationView = (NavigationView) findViewById(R.id.navigation);
and then you can access and change all properties associated to it, including layout_width and height ;)
Apply similar to this:
<?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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ExpandableListView
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_main2"
app:menu="#menu/activity_main2_drawer" />
</android.support.v4.widget.DrawerLayout>

main Layout Overlaps with Navigation Drawer

i am new with navigation drawer. I want to Create layout for my mainactivity, but why overlap so my main layout not visible, may you help me. Thanks for answer
This is my activity_main.xml layout :
<?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"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:background="#drawable/splashscreen">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jaakkk"
android:textColor="#ffffff" />
</FrameLayout>
<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"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
How can I do to change that?
So, your problem is drawer layout overlapping the linear layout below. Use
u can add this line in
<android.support.design.widget.NavigationView
android:layout_below="#id/drawer_layout"/>

My emulator shows just Listview on entire screen. even its width is limited to 240dp

I am using drawer layout and Listview is using as drawer.
My emulator just showing Listview as width set to match parent even it's set to 240dp.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer Layout"
android:background="#000000">
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/drawerList"
android:background="#color/colorAccent"/>
<FrameLayout
android:layout_width="250dp"
android:layout_height="match_parent"
android:id="#+id/fragmentContainer"
android:background="#ffffff"
android:choiceMode="singleChoice"
android:dividerHeight="0dp"
android:divider="#android:color/transparent"/>
</android.support.v4.widget.DrawerLayout>
Drawer layouts are usually hidden outside of the screen until you open them. Try opening the drawer by swiping from the side.
You must use NavigationView inside DrawerLayout and limit width in NavigationView.
Example layout structure is like that , there is 2 different usage of NavigationView :
<?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"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- you can add extra views in DrawerLayout -->
<include layout="#layout/content_home"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start"
app:menu="#menu/homepage_leftdrawer"/>
<!-- you can use listview, recyclerview etc in navigationview -->
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
app:headerLayout="#layout/homepage_right_header"
android:layout_gravity="end">
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:layout_marginTop="80dp" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

How to add other layout within NavigationView

I'm trying to add Expandlistview within my NavigationView but it fails.
Can I add layout below header?
activity_main layout
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/header">
<ExpandableListView
android:id="#+id/expanded_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
My solution would be .. if you are using header layout with specific height, for ex 200dp.. then in activity_main.xml include margin top 0f 210dp for expandable listview tag.. I hope you are not using menu for navigation view
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/header">
<ExpandableListView
android:id="#+id/expanded_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="210dp" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
you can use listview in navigation drawer list and append header view to the list which contains expandable listview. I am not sure but you can check it

Categories

Resources