Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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>
like image 733
lopez.mikhael Avatar asked Jun 25 '15 12:06

lopez.mikhael


People also ask

How do I add a navigation drawer to an existing activity?

To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.

How do you use a navigation drawer?

The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout.

What is the navigation drawer?

Navigation drawers provide access to destinations and app functionality, such as switching accounts. They can either be permanently on-screen or controlled by a navigation menu icon. Navigation drawers are recommended for: Apps with five or more top-level destinations.

What is NavigationView in Android?

com.google.android.material.navigation.NavigationView. Represents a standard navigation menu for application. The menu contents can be populated by a menu resource file. NavigationView is typically placed inside a DrawerLayout .


2 Answers

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);
    }
    
like image 136
Evans Mauta II Avatar answered Oct 05 '22 23:10

Evans Mauta II


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... :-)

like image 41
Azim Ansari Avatar answered Oct 05 '22 23:10

Azim Ansari