I have created one NavigationView inside DrawerLayout using Android Design Support Library
<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"
android:fitsSystemWindows="true">
<!-- other views -->
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
my_navigation_items.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/bookmarks_drawer"
android:icon="@drawable/ic_drawer_bookmarks"
android:title="@string/bookmarks" />
<item
android:id="@+id/alerts_drawer"
android:icon="@drawable/ic_drawer_alerts"
android:title="@string/alerts" />
<item
android:id="@+id/settings_drawer"
android:icon="@drawable/ic_drawer_settings"
android:title="@string/settings" />
</group>
</menu>
Now, I want to set unread notification counter for each item of NavigationView
like below image:
how to set unread notification counter on item of NavigationView
?
Support for this was added in 23 of the design library, I believe.
In your menu XML file, set a
actionLayout
in theapp
XML prefix:Then have the menu included layout calculate the unread items, probably using a custom view or fragment to fetch the data.
I would suggest you remove the NavigationView and add the navigation drawer elements as a fragment i.e. in the DarawerLayout file
Then create a class for the fragment and create a resource file for the class that will contain the elements for your drawer i.e.
You can then add this fragment to your main activity i.e.
the "set up" method is located in the fragment and that's where you initialize it i.e.
In the layout file for the drawer, add the elements you want to set the unread notification for then add a relative layout whose orientation you'll set to vertical i.e.
Within this vertical relative layout, add another relative layout for the elements. This would be the place you'd add the "Alerts" section as in the image you posted. This relative layout should be horizontal i.e.
The last textview element in the code just above this will contain the counter you want to add for unread notifications. In the xml its color is set to red. From here, get a reference to it in the Navigation drawer fragment class using its ID (in oncreateview) and populate it with your counter.
Hope this helps!
NavigationView
is designed to be an easy way to implement a basic navigation drawer that is compliant with the Material design guidelines.If you want anything more than a basic navigation drawer (i.e. one with text navigation items and an optional header), you will need to create your own layout for your navigation drawer.
I found this easy to implement solution on this web site https://android.jlelse.eu/android-adding-badge-or-count-to-the-navigation-drawer-84c93af1f4d9 Follow the steps below
Step 1: Create an Android Studio project, instead of choosing empty activity select “Navigation Drawer Activity”.
Step 2: Adding “actionViewClass” attribute to the navigation drawer menu (i.e. menu/youractivityname_drawer.xml)
Step 3 : Declare the Navigation Drawer menu item and initialize the item with the badge value. In your main Activity, declare the menu item of the Navigation Drawer as given below
Step 5: Initialize initializeCountDrawer() where ever it’s required. It can also be used to update the count or badge value in the navigation drawer menu item.
Source: https://android.jlelse.eu/android-adding-badge-or-count-to-the-navigation-drawer-84c93af1f4d9
Updated answer:
Using
app:actionLayout
with support library 23.1.1 or higher will support the custom layout as below.Create your custom counter layout like below.
menu_counter.xml:
Reference it in your drawer menu item in xml.
menu/drawer.xml:
Note that you should use
app
namespace, don't try to useandroid
.Alternatively you can manually set action view with
MenuItem.setActionView()
method.Find menu item and set counter like below code:
Note, that you will need to use
MenuItemCompat
if you have to supportAndroid 2.x
versions.Previous Answer (For older version) :
Solved with
ListView
insideNavigationView
as below code...In your activity set list item as below...
The click listener for ListView in the navigation drawer...
MenuAdapter..java
drawer_list_item.xml
SlideMenuItem.java