I'm using the Designs Support Libraries NavigationView like this:
<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_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<!-- put your main layout here -->
<include layout="#layout/drawer_main_layout"/>
<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"/>
</android.support.v4.widget.DrawerLayout>
And I have set this menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_dashboard"
android:title="Home" />
<item
android:id="#+id/nav_messages"
android:icon="#drawable/ic_event"
android:title="Messages" />
<item
android:id="#+id/nav_friends"
android:icon="#drawable/ic_headset"
android:title="Friends" />
<item
android:id="#+id/nav_discussion"
android:icon="#drawable/ic_forum"
android:title="Discussion" />
</group>
<item android:title="Sub items">
<menu>
<item
android:icon="#drawable/ic_dashboard"
android:title="Sub item 1" />
<item
android:icon="#drawable/ic_forum"
android:title="Sub item 2" />
</menu>
</item>
</menu>
Is there any way to use the NavigationView with a layout rather than a menu?
Here's how I solved it, and worked perfectly:
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/nav_header" />
<ListView
android:id="#+id/lst_menu_items"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.design.widget.NavigationView>
Yes You can ....
As I have done ...
Just take your custom layout inside the NavigationView
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
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/navView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">
<ListView
android:entries="#array/test"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
The above code work for me...
But don't forget to remove app:menu from NavigationView . Otherwise it will overlap your custom view on the menu items.
Related
This question already has answers here:
NavigationView get/find header layout
(9 answers)
Closed 1 year ago.
I have done my best to reproduce a UI design for an android navigation drawer.
I have already designed the nav header to look like what is in the UI design.
When I run the app, the header (the layout in nav_header_home.xml) is not visible in the simulator.
The nav_header_home.xml for the navigationdrawer:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/clientPrimaryBlue"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#drawable/home" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Home"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
</LinearLayout>
activity_home_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<item android:title="Movies">
<menu>
<item
android:id="#+id/nav_action"
android:icon="#drawable/ic_menu_share"
android:title="Action" />
<item
android:id="#+id/nav_adventure"
android:icon="#drawable/ic_menu_send"
android:title="Adventure" />
<item
android:id="#+id/nav_comedy"
android:icon="#drawable/ic_menu_send"
android:title="Comedy" />
<item
android:id="#+id/nav_crime"
android:icon="#drawable/ic_menu_send"
android:title="Crime" />
<item
android:id="#+id/nav_drama"
android:icon="#drawable/ic_menu_send"
android:title="Drama" />
<item
android:id="#+id/nav_fantasy"
android:icon="#drawable/ic_menu_send"
android:title="Fantasy" />
</menu>
</item>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_actors"
android:icon="#drawable/ic_menu_camera"
android:title="Actors" />
<item
android:id="#+id/nav_directors"
android:icon="#drawable/ic_menu_gallery"
android:title="Directors" />
<item
android:id="#+id/nav_producers"
android:icon="#drawable/ic_menu_slideshow"
android:title="Producers" />
</group>
<item android:title="My Account">
<menu>
<item
android:id="#+id/nav_update_profile"
android:icon="#drawable/ic_menu_share"
android:title="Update Profile" />
<item
android:id="#+id/nav_reset_password"
android:icon="#drawable/ic_menu_send"
android:title="Reset Password" />
<item
android:id="#+id/nav_sign_out"
android:icon="#drawable/ic_menu_send"
android:title="Sign Out" />
</menu>
</item>
</menu>
Screenshot of expected menu layout
Screenshot of layout in android emulator
app_bar_home.xml:
<?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"
tools:context="media.client.clientmediaandroid.activity.home.HomeActivity">
<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>
<include layout="#layout/content_home" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
action_home.xml with the DrawerLayout:
<?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_home"
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_home"
app:menu="#menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
With help from #MikeM., I was able to adjust my layout settings in a way that allowed me to see the navigation header.
His contribution can be read directly here -
Home Navigation header of NavigationDrawer not showing in simulator
Set the fitsSystemWindows attributes to false on the DrawerLayout and NavigationView as suggested by Mike. M.
im beginer of android. i design a navigation drawer like this:
as you see all items in menu are on the right Except "SubHeader".
how can i move "SubHeader" item to the right of this menu? i use all properties like gravity, layout gravity, layoutDirection to the SubHeaderItem but it doesn't work.
my code is
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/navigation_view"
android:layout_gravity="end"
android:fitsSystemWindows="true"
android:layoutDirection="rtl"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu"
/>
my menu code is:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:checkableBehavior="single">
<item
android:title="inbox"
android:id="#+id/inbox_id"
android:icon="#drawable/inbox" />
</group>
<item android:title="SubHeader">
<menu>
<item
android:title="all main"
android:id="#+id/all_mail"
android:icon="#drawable/allmail" />
</menu>
</item>
</menu>
Try adding these attributes to NavigationView in your layout file.
android:layoutDirection="rtl"
android:textDirection="rtl"
If no other way. You should use custom layout
example:
<android.support.design.widget.NavigationView
android:layoutDirection="rtl"
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer>
<LinearLayout>
// code xml here
</LinearLayout>
<android.support.design.widget.NavigationView/>
try this make your menu gravity="right" of your menu item
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="end"
android:layoutDirection="rtl"
android:layout_gravity="end">
<group android:checkableBehavior="single"
android:layout_gravity="end"
android:gravity="end">
<item
android:id="#+id/nav_1"
android:icon="#drawable/abc"
android:gravity="end"
android:layout_gravity="end"
android:title="menu title"/>
</group>
</menu>
and make your navigation view like this
<android.support.design.widget.NavigationView
android:layoutDirection="rtl"
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
I want to set navigation menu text color to white how can able do it??
I want to make screen like this
Please check my following code snippet
My Navigation Menu Looks Like:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/nav_timer"
android:icon="#drawable/ic_av_timer_black_36dp"
android:title="Timer"
android:textColor="#000000"
/>
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_account_circle_black_48dp"
android:title="Profile"
>#color/menuColor</item>
<item
android:id="#+id/nav_logs"
android:icon="#drawable/ic_assignment_black_48dp"
android:title="Logs" />
<item
android:id="#+id/nav_changeBusiness"
android:icon="#drawable/ic_business_black_48dp"
android:title="Change Business" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_48dp"
android:title="Logout" />
Add app:itemTextColor="#android:color/white" in your navigation view.
<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:itemTextColor="#android:color/white"
android:fitsSystemWindows="true"
app:menu="#menu/act_home_drawer_menu"
/>
It will give you desired result.
You can add a Listview under Navihation View. And then you can add your custom adapter with textview.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
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=".MainActivity">
<FrameLayout
android:id="#+id/content_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/main_toolbar" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"/>
<ListView
android:id="#+id/menuList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>
This will solve your problem. It worked for me.
I have a serious doubt. how can i add the expandable list button on the right end of the navigation drawer menu item in the android studio? for further details let me post my image...
I have just edited my post...
below is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/view">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
android:background="#color/red"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/tabLayout"
android:background="#color/red"
app:tabIndicatorColor="#color/white"
app:tabTextAppearance="#style/MyStyle"
android:layout_below="#+id/toolbar"
android:scrollbars="horizontal"
android:layout_alignParentLeft="true">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/viewPager">
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
and here is my activity_navigation.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.goserwizz.NavigationActivity">
<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>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_container">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/header"
app:menu="#menu/menu_navigation"/>
</android.support.v4.widget.DrawerLayout>
And here is my menu_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/Book_my_services"
android:title="Book my services"
android:icon="#drawable/book_my_service"/>
<item
android:id="#+id/My_services"
android:title="My services"
android:childIndicatorRight="30dp"
android:icon="#drawable/services" />
<item
android:id="#+id/Add_your_vehicles"
android:title="Add your vehicles"
android:icon="#drawable/add_vehicle"/>
<item
android:id="#+id/Reschedule_slot"
android:title="Reschedule slot"
android:icon="#drawable/slot" />
<item
android:id="#+id/Cancel_services"
android:title="Cancel services"
android:icon="#drawable/cancel_service"/>
<item
android:id="#+id/history"
android:title="History"
android:icon="#drawable/history"/>
<item
android:id="#+id/Payment"
android:title="Payment"
android:icon="#drawable/payment"/>
</group>
<item android:title="HELP">
<menu>
<item
android:id="#+id/Emergency_assistant"
android:title="Emergency Assistant"
android:icon="#drawable/emergency" />
<item
android:id="#+id/Call_support"
android:title="Call support"
android:icon="#drawable/call_support" />
<item
android:id="#+id/Settings"
android:title="Settings"
android:icon="#drawable/settings" />
</menu>
</item>
<item android:title="MORE">
<menu>
<item
android:id="#+id/feedback"
android:title="Feedback"
android:icon="#drawable/feedback" />
<item
android:id="#+id/How_it_works"
android:title="How it works"
android:icon="#drawable/how_it_work"/>
<item
android:id="#+id/FAQ"
android:title="FAQ"
android:icon="#drawable/faq"/>
<item
android:id="#+id/terms_and_conditions"
android:title="Terms and conditions"
android:icon="#drawable/terms_condition"/>
<item
android:id="#+id/Privacy_policy"
android:title="Privacy policy"
android:icon="#drawable/privacy"/>
<item
android:id="#+id/About"
android:title="About"
android:icon="#drawable/about"/>
</menu>
</item>
</menu>
Now,where should i make the changes so, that i can get the expandable list button on the right side of navigation menu item.
Currently my navigation drawer looks similar to this
But i need my navigation drawer like this
I need expandable list button on the right side of navigation menu item. Kindly help me ...Many thanks in advance.....
<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">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/navigation_drawer"
android:name="com.rooftap.adapterClass.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Customize your fragment_navigation_drawer.xml layout add spinners or whatever you want like how we do in normal layout and create a NavigationDrawerFragment.class for actions. don't use android.support.design.widget.NavigationView.
I implement ExpandableListView but i have also items in menu xml, so how can i declare that this list view should be below of one of the item inside of my menu.xml? For example if i want expandablelistview below location item what should i do? Here is my code:
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="#+id/location" android:icon="#drawable/mylocation" android:title="#string/My_Location" android:animateLayoutChanges="true"/>
</group>
<item android:title="#string/action_settings">
<menu>
<group android:checkableBehavior="single">
<item android:id="#+id/profile" android:icon="#drawable/settings"
android:title="#string/Edit_Profile" android:animateLayoutChanges="true" />
<item android:id="#+id/logout" android:icon="#drawable/logout"
android:title="#string/Log_Out" android:animateLayoutChanges="true"/>
</group>
</menu>
</item>
</menu>
And drawer_main.xml
<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/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="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="left|start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
android:nestedScrollingEnabled="true"
app:itemIconTint="#FFFFFF"
app:itemTextColor="#FFFFFF"
android:background="#000000">
<FrameLayout
android:id="#+id/drawer_list_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ExpandableListView
android:id="#+id/navList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
So my problem is that the ExpandableListView remain top and i dont want to use layout_marginTop because it depends on user screen. Is there any other way to accomplish this issue?