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>
Related
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"/>
I have used two color: light color #607D8B for app bar and dark color #455A64 for above appbar. But problem is, navigation drawer display two different color other then I mentioned. I don't know why the different color come to the navigation drawer. So, what can be done to solve this issue ?
content_main.xml
<?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_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="80dp"
android:background="#455A64"
android:fitsSystemWindows="true"
app:menu="#menu/activity_main_drawer">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="#+id/ex_list_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:childDivider="#00000000"
android:divider="#null"
android:groupIndicator="#null" />
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
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>
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.
I'm trying to add my own custom header view into Google's new NavigationView but for some reason, no matter what I do, Android Studio gives me a warning saying include and RelativeLayout is not allowed here. I tried doing
<android.support.design.widget.NavigationView
android:id="#+id/navigation_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<include layout="#layout/nav_drawer_header" />
<android.support.v7.widget.RecyclerView
android:id="#+id/nav_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccc"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"/>
</android.support.design.widget.NavigationView>
but only the RecyclerView shows up and I'm not sure what else to do.
I would really appreciate some help. Thanks!
You can add headers to a NavigationView using the app:headerLayout XML attribute:
<android.support.design.widget.NavigationView
android:id="#+id/navigation_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_drawer_header" />
Or programatically via inflateHeaderView()
Below demonstrates a typical usage of NavigationView in the Design Support Library. As you can see, the NavigationView have an attribute called headerLayout to set its header 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content -->
<FrameLayout
android:id="#+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- Drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer"/>
</android.support.v4.widget.DrawerLayout>