NavigationView menu items with counter on the right
Starting from version 23 of appcompat-v7 NavigationView
supports action views, so it is quite easy to implement counter yourself.
Create counter layout, i.e.
menu_counter.xml
:<?xml version="1.0" encoding="utf-8"?>
<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:textAppearance="@style/TextAppearance.AppCompat.Body2" />Reference it in your drawer menu xml, i.e.
menu/drawer.xml
:<item
...
app:actionLayout="@layout/menu_counter" />
Note that you should use app
namespace, don't try to use android
.
Alternatively you can manually set action view with MenuItem.setActionView()
method.
Find menu item and set counter:
private void setMenuCounter(@IdRes int itemId, int count) {
TextView view = (TextView) navigationView.getMenu().findItem(itemId).getActionView();
view.setText(count > 0 ? String.valueOf(count) : null);
}
Note, that you will need to use MenuItemCompat if you have to support Android 2.x versions.
How to add notification value for item on NavigationView for Material Design Drawer?
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 theapp
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);
}
How to put counter on NavigationView from Android Design Library?
In new NavigationDrawer they have not mention an about the counter you are lookig for. But you can avoid the older way to put counter.
Because Navigation can contain any child view so you can use it like this.
<android.support.design.widget.NavigationView
android:id="@+id/navView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
<ListView
android:entries="@array/test"
android:id="@+id/lvData"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.NavigationView>
So just reference your listview and give the customize layout as you want.
I know it is Older way of listview. But it will reduce the code of NavigationDrawer. I hope it helps you.
Note : Never use app:menu and child view in NavigationView at the same time. Otherwise it will overlap each other.
Style not getting applied to NavigationView menu in NavigationDrawer
so i figured out the issue, i was applying style
the wrong way in NavigationView
. Here is what i changed
before
style="@style/NavigationDrawerStyle"
after
app:itemTextAppearance="@style/NavigationDrawerStyle"
Anchoring a PopupMenu to Navigationview menu items
So I figured you can just create an empty view inside the menu item as follows:
create a layout xml file including only the following:
<?xml version="1.0" encoding="utf-8"?>
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
lets call it "view_empty
".
Now the menu item should have that empty view layout included inside of it by adding the following line to its item: app:actionLayout="@layout/drawer_empty"
My item looks as follows:
<item
android:id="@+id/menubutton_submenuButton"
android:title="example menu item"
app:actionLayout="@layout/view_empty"/>
Now then, all that's left is just using it:
//onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menubutton_submenuButton:
MenuItem item = drawerMenu.findItem(R.id.menubutton_submenuButton);
PopupMenu popupMenu = new PopupMenu(this, MenuItemCompat.getActionView(item));
popupMenu.getMenuInflater().inflate(R.menu.sub_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
//handle the inflated menu's buttons here
return true;
}
});
popupMenu.show();
break;
What'd happen now is that the PopupMenu would anchor itself to an invisible view inside the menuItem, thus making it look like the container menuItem is the anchor point.
Related Topics
What's Better? Notifydatasetchanged or Notifyitemchanged in Loop
Android Gcm: Same Sender Id for More Application
Sending Intent to Broadcastreceiver from Adb
Generate Signed APK Android Studio
Session 'App': Error Launching Activity
Textinputlayout Not Showing Edittext Hint Before User Focus on It
Get Focused View from Viewpager
Detecting Which Selected Item (In a Listview) Spawned the Contextmenu (Android)
Android: Crop an Image After Taking It with Camera with a Fixed Aspect Ratio
How to Know That My Webview Is Loaded 100%
How to Resolve "Missing Pendingintent Mutability Flag" Lint Warning in Android API 30+
Programmatically Add Id to R.Id
No Good Example About Recyclerview and Staggeredgridlayoutmanager in Android Docs
How to Set a Viewpager Inside a Fragment
Android Imageview: Setting Drag and Pinch Zoom Parameters