And I use in the application NavigationDrawer from the library of support

 compile 'com.android.support:design:24.0.0' 

Here is the XML

 .... <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_second" app:menu="@menu/activity_third_drawer" /> .... 

The menu that I install here

 app:menu="@menu/activity_third_drawer" 

Here is the XML code

 <?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_inbox" android:icon="@drawable/ic_mail_outline_white" android:title="@string/inbox" /> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_close_24dp" android:title="@string/gallery" /> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_close_24dp" android:title="@string/slideshow" /> <item android:id="@+id/nav_manage" android:icon="@drawable/ic_close_24dp" android:title="@string/tools" /> </group> <item android:title="@string/about_app"> <menu> <item android:id="@+id/nav_about" android:icon="@drawable/ic_close_24dp" android:title="@string/about1" /> </menu> </item> </menu> 

And in the end it looks like this

enter image description here

I want when I get messages in the inbox, so that it is noted in NavigationDrawer

Like this

enter image description here

Different libraries can do this. I chose this one.

She can cling badges anywhere, but there is not a single example of how to attach to <item> ... There is an example of how to hook into XML

 <com.readystatesoftware.viewbadger.BadgeView android:id="@+id/badge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="OK" /> 

But I still do not understand how to attach it to <item> .

Can there be other solutions how to do this?

Debag

enter image description here

onPrepareOptionsMenu

 @Override public boolean onPrepareOptionsMenu(Menu menu) { View menuItemView = MenuItemCompat.getActionView(navigationView.getMenu().findItem(R.id.nav_inbox)); BadgeView badge = new BadgeView(context, menuItemView); badge.setText("1"); badge.show(); return super.onPrepareOptionsMenu(menu); } 

    2 answers 2

    Try this:

     NavigationView nv = ...; View menuItemView = MenuItemCompat.getActionView(nv.getMenu().findItem(R.id.menu_action_item)); BadgeView badge = new BadgeView(context, menuItemView); badge.setText("1"); badge.show(); 
    • Similarly, Yuri, I did not think that I could find this twist from the code ... Only why I don’t find the menuItemView I did the debug and it shows that after the execution of View menuItemView = navigationView.findViewById(R.id.nav_inbox); of this command menuItemView = null ... I think this is probably due to the fact that the NavigationDrawer closed and the item itself has not yet been created ... But what about the opening of that listener ...? - Aleksey Timoshchenko
    • I just tried to execute the code with the NavigationDrawer open and still returns null ... Attached a screen to the question. Strange - Aleksey Timoshchenko
    • @AlekseyTimoshchenko, updated code. And yes - you need to do this only after creating the menu. In OnResume or when opening the left menu, for example. - Yuriy SPb
    • It returns null anyway ... I do debug I open the NavigationView launch the method and see that menuItemView = null is all the same ... Although you somehow told me how to get to header a and everything worked ... That's it View header = navigationView.getHeaderView (0); TextView textEmail = (TextView) header.findViewById (R.id.textEmail); textEmail.setText (email); - Aleksey Timoshchenko 2:47 pm
    • one
      On item, you can hang the app: actionLayout and add the Badge to the existing icon and textView. That is, the actionLayout will contain an ImageView (icon) TextView (menu name) and Badge - Gikas

    In the end, I used this library

     compile('com.mikepenz:materialdrawer:5.3.6@aar') { transitive = true } 

    On the gita is fully described how to use it ...