lopez.mikhael
lopez.mikhael

Reputation: 10071

How to add notification value for item on NavigationView for Material Design Drawer?

My question is very simple, how to add a notification value right of the item on NavigationView for Material Design Drawer like that ?

enter image description here

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

Answers (5)

Evans Mauta II
Evans Mauta II

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"/>
  1. 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"
        />
    
  2. 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

Azim Ansari
Azim Ansari

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

thiagolr
thiagolr

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

user3704147
user3704147

Reputation: 11

You could use a library for this. Try MaterialNavigationDrawer or MaterialDrawer

Upvotes: 0

Rose R.
Rose R.

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

Related Questions