I have used the following tutorial to implement material design drawer.
http://www.androidhive.info/2015/04/android-getting-started-with-material-design/
But i want to add sections in the drawer after 2 items. How to make the recyclerview display sections and subheaders?
You better use the design support library to do this.
specifically android.support.design.widget.NavigationView class
Checkout the following links for more
Tutorial
Github sample
Some code snippets from sample:
activity_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"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/include_list_viewpager"/>
<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 menu shown on drawer will have a separator at the end of the first menu group.
drawer_view.xml
<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_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>
Related
In my app I want an expandable list view, only one item and expandable list item comes from mysql database, with a click on the item it opens a fragment. I have searched many answers, but nothing work for me, I want when clicking on a category the list shows which data comes from a database. And when clicking on any item it works like a drawer item click.
I want this category as expandable listview.
drawer.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/home"
android:icon="#drawable/ic_menu_home"
android:title="Home" />
<item
android:id="#+id/wishlist"
android:icon="#drawable/ic_menu_wishlist"
android:title="Wish List" />
<item
android:id="#+id/category"
android:icon="#drawable/ic_menu_category"
android:title="Category"/>
<item
android:id="#+id/order"
android:icon="#drawable/ic_menu_shopping_basket"
android:title="Your Orders" />
<item
android:id="#+id/cart"
android:icon="#drawable/ic_menu_shopping_cart"
android:title="Your Cart" />
</group>
<item android:title="Activity">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/account"
android:icon="#drawable/ic_menu_person"
android:title="Your Account" />
<item
android:id="#+id/logout"
android:icon="#drawable/ic_menu_logout"
android:title="Logout" />
</menu>
</item>
activity.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/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" >
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/nav_header_height"
android:background="#color/silver"
android:dividerHeight="0dp"
android:groupIndicator="#drawable/ic_menu_expand"
android:transcriptMode="alwaysScroll"/>
</android.support.design.widget.NavigationView>
There is library named MaterialDrawer.
It is really simple to use and it can satisfy your expectations.
I tried to set margin to default navigation drawer's "divider" attribute. I found many approach in Stackoverflow form but none of the answers are adequate.
I could create a custom navigation drawer list without using /menu/activity_main_drawer.xml. That looks like what I want. But I have concerns about the performance of the application.
I want to add margin to default divider. Is there a way to customize default navigation drawer divider?
That is my customized divider. Everything is perfect.
That is the default navigation drawer's divider which I could not give any margin.
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:listDivider">#color/colorAccent</item>
...
</style>
divider_layout.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="60dp"
android:insetRight="8dp" >
<shape>
<solid android:color="#c0c0c0" />
</shape>
</inset>
activity_main_drawer.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/nav_camera"
android:icon="#drawable/rakipicon"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
</menu>
activity_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/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="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:divider="#drawable/divider_layout"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
I know it's been a while, but for anyone that might stumble upon this, I had the same issue and resolved it like this.
Where you define the navigation view, you can just set padding horizontal, which will set a padding for the entire navigation view. This will apply to the dividers as well.
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingHorizontal="24dp">
You can create a drawable named menu_divider.xml in your resources folder:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#c0c0c0"
android:left="16dp"
android:right="16dp" />
and set your navigation view to use it:
<style name="ThemeOverlay.App.NavigationView" parent="">
<item name="android:listDivider">#drawable/menu_divider</item>
</style>
Just add this in the drawer layout:
app:dividerInsetEnd="25dp"
app:dividerInsetStart="57dp"
Like below:
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:dividerInsetEnd="25dp"
app:dividerInsetStart="57dp"
app:elevation="32dp"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#color/gray"
app:itemTextColor="#color/gray"
app:menu="#menu/activity_main_drawer">
I have the below design to implement for the Android Navigation App Drawer of the app and I am confused how to go about it.
In my design, there is a black strip on the left side, which also has an image. My query is how do I create that space in the app drawer ?
I tried to add a Layout inside the app drawer but couldn't. It only allows 'Items' and 'Groups' to be added. What are my possible options ?
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
android:background="#color/colorBg"
<group android:checkableBehavior="single">
android:background="#color/colorBg"
<item
android:id="#+id/myasset"
android:background="#color/colorBg"
android:title="My Asset" />
<item
android:id="#+id/account"
android:background="#color/colorBg"
android:title="Account" />
<item
android:id="#+id/howitworks"
android:background="#color/colorBg"
android:title="How it works" />
<item
android:id="#+id/feedback"
android:background="#color/colorBg"
android:title="Feedback" />
<item
android:id="#+id/legal"
android:background="#color/colorBg"
android:title="Legal" />
<item
android:id="#+id/exit"
android:background="#color/colorBg"
android:title="Exit" />
</group>
</menu>
When you create new project in Android Studio, there is an option that help you build NavigationDrawer.
Default activity_main.xml in layout
<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="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" />
And you can do anything with activity_main_drawe or nav_header_main.
You can create custom navigation drawer. Try below tutorial :
Custom navigation drawer 1
Custom navigation drawer 2
The solution that worked for me is Hamzeh Soboh's answer here.
https://stackoverflow.com/a/31716815/609235
I need your help please help me.
I have a navigation drawer in my app. i want to move navigation drawer to right completely . I moved the navigation itself but i want to move menu items to right too.
My Screenshot of Navigation Drawer
Activity_main:
<?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="fill_parent"
android:layout_height="match_parent"
android:layout_marginleft="100dip"
android:fitsSystemWindows="true"
tools:openDrawer="end">
<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="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
My items of menu in xml :
Activity_Main_Drawer:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/nav_home" />
<item
android:id="#+id/nav_photos"
android:icon="#drawable/ic_photo_library_black_24dp"
android:title="#string/nav_photos" />
<item
android:id="#+id/nav_movies"
android:icon="#drawable/ic_local_movies_black_24dp"
android:title="#string/nav_movies" />
<item
android:id="#+id/nav_notifications"
android:icon="#drawable/ic_notifications_black_24dp"
android:title="#string/nav_notifications" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings_black_24dp"
android:title="#string/nav_settings" />
</group>
<item android:title="Other">
<menu>
<item
android:id="#+id/nav_about_us"
android:title="#string/nav_about_us" />
<item
android:id="#+id/nav_privacy_policy"
android:title="#string/privacy_policy" />
</menu>
</item>
</menu>
first: You can put your device in RTL mode, go to develop settings (Force RTL layout direction).
After doing this all the layouts will mirror correctly.
If you don't want to change the whole device into RTL use this code in onCreate method of mainactivity before super and before you inflate the content view
window.decorView.layoutDirection = View.LAYOUT_DIRECTION_RTL
You need to use a custom layout for NavigationView.
According to this: http://www.kamilslesinski.com/home/navigation-drawer-customising-navigationview-items you can override the layout by copying original file and customizing it, or you can add a reference to your custom layout, like that:
how to customize snackBar's layout?
in refs.xml.
I'm designing a native navigation drawer in Android Studio.
I can't see the drawer in my preview because it is sitting left of the activity, out of range of the preview.
For now I'm using a testlayout.xml file to see my changes, but a lot of times I forget to copy paste them into the right activity.
Is there a way to preview the drawer layout?
If you want to be able to preview the drawer layout / navigation bar
If you are following the tutorial at https://developer.android.com/training/implementing-navigation/nav-drawer.html
You can put your listview for the nav drawer in a different xml file in the /layout folder, then include it in the drawer layout xml. Means you can edit it in the drawer_layout file and be able to see it properly
layout/drawer_layout
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/left_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#color/nav_drawer_background_color"/>
Then in the navbar
<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">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- The navigation drawer -->
<include
android:id="#+id/listView"
layout="#layout/drawer_layout"/>
</android.support.v4.widget.DrawerLayout>
To see NavigationView preview add tools namespace to your drawer layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
then add attribute tools:openDrawer equal to gravity of layout_gravity value of your Navigation view, for example
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:openDrawer="left">
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
app:headerLayout="#layout/navigation_header_main"
app:itemTextAppearance="#style/NavigationItemStyleLight"
app:menu="#menu/navigation_main_menu"/>
</android.support.v4.widget.DrawerLayout>
<?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">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
Just Add
tools:showIn="navigation_view"
You can use NavigationView to get its preview on AndroidStudio, Its much better and easier to implement then the old NavigationDrawer.
activity_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/prof_app_bar"
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:itemTextColor="#color/text_head_color"
app:itemIconTint="#null"
app:headerLayout="#layout/nav_header_main2"
app:menu="#menu/professional_menu" />
</android.support.v4.widget.DrawerLayout>
Define the menu in navigationmenu.xml place it in menu resource folder:-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single"
android:id="#+id/top_menu">
<item
android:id="#+id/pnav_setting"
android:icon="#drawable/accountsettings"
android:title="#string/paccount_settingss" />
<item
android:id="#+id/pnav_expertise"
android:icon="#drawable/professional"
android:title="#string/pexpertise" />
<item
android:id="#+id/pnav_comu"
android:icon="#drawable/communication"
android:title="#string/pcomunication" />
<item
android:id="#+id/pnav_change_pass"
android:icon="#drawable/changepasswd"
android:title="#string/pchange_pass" />
<item
android:id="#+id/pnav_change_lang"
android:icon="#drawable/changelang"
android:title="#string/pchange_lang"
android:visible="true"/>
</group>
<group android:checkableBehavior="single"
android:id="#+id/middle_menu">
<item
android:id="#+id/pnav_about_us"
android:icon="#drawable/aboutus"
android:title="#string/about_us" />
<item
android:id="#+id/pnav_feedback"
android:icon="#drawable/feedback"
android:title="#string/feedback"
android:visible="false"/>
<item
android:id="#+id/nav_privacy_policy"
android:icon="#drawable/privacy"
android:title="#string/privacy_policy" />
<item
android:id="#+id/pnav_faq"
android:icon="#drawable/faq"
android:visible="false"
android:title="#string/faq" />
<item
android:id="#+id/pnav_contact_us"
android:icon="#drawable/contactus"
android:title="#string/contact_us" />
</group>
<group android:checkableBehavior="single"
android:id="#+id/bottom_menu">
<item
android:id="#+id/pnav_sign_out"
android:icon="#drawable/signout"
android:title="#string/sign_out" />
</group>
</menu>
And, for getting click events use this:-
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(yourItemclickListener);