Reputation: 10071
My question is very simple, how to add a notification value right of the item on NavigationView for Material Design Drawer like that ?
Is there a property in the menu items for defining the drawer?
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:checkableBehavior="single">
<item
android:id="@+id/drawer_home"
android:checked="true"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/home"/>
<item
android:id="@+id/drawer_favourite"
android:icon="@drawable/ic_favorite_black_24dp"
android:title="@string/favourite"/>
...
<item
android:id="@+id/drawer_settings"
android:icon="@drawable/ic_settings_black_24dp"
android:title="@string/settings"/>
</group>
</menu>
Upvotes: 21
Views: 6523
Reputation: 208
This is possible with NavigationView
from Version 23 of AppCompat-V7 using action views.
1. Create a layout for the counter e.g. nav_drawer_counter.xml
:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="@color/colorPrimary"/>
Add a reference to it from each item you'd like to show a counter value for in your menu/nav_drawer.xml
(ensure you use the app
namespace):
<item
...
app:actionLayout="@layout/activity_main_nav_drawer_menu_counter"
/>
Add a method to set a value to the TextView
e.g:
@Override
protected void onCreate(Bundle savedInstanceState) {
....
setNavItemCount(R.id.nav_notifications, 10);
}
private void setNavItemCount(@IdRes int itemId, int count) {
TextView view = (TextView) navigationView.getMenu().findItem(itemId).getActionView();
view.setText(count > 0 ? String.valueOf(count) : null);
}
Upvotes: 15
Reputation: 1378
Oh Yes... You can change that counter value.
From the images given by you it looks live you are using Rudson Lima's Material Design Navigation Drawer
In its library there is a function public void setNewCounterValue(int drawerItemPosition, int counterValue);
inside the class NavigationLiveo
.
Hope you will use its object properly to call this function.
Best of Luck... :-)
Upvotes: 5
Reputation: 7027
You should check exactly how it was done on the original project's github:
https://github.com/rudsonlive/NavigationDrawer-MaterialDesign/ https://play.google.com/store/apps/details?id=br.liveo.navigationliveo
Basically you create a custom view for the ListView's lines and update them accordingly.
Upvotes: 1
Reputation: 11
You could use a library for this. Try MaterialNavigationDrawer or MaterialDrawer
Upvotes: 0
Reputation: 141
From some research, I have found that, no, there is no real way to add the notifications to the drawer. However, the example shown in the Creating a Navigation Drawer tutorial at Developers shows how to force a redraw every time it is opened (and also how to implement it with the Menu XML sheets). It does not go into detail about how to add the notifications, but there are other sources for that.
This tutorial is another "how to make a navigation drawer" tutorial with the bonus of exact details on how the adapted the ListView adapters to suit a notification button. I think this is more than likely what you are looking for.
Good luck! I hope that helps.
Upvotes: 0