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>
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.
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.
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.
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 .
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);
}
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... :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With