android - NavigationView menu items with counter on the right -
the new navigationview in new design support library works great.
they use "menu-items" display options.
but how can display counter right of menu item?
like in picture:
or in gmail app.
starting version 23 of appcompat-v7 navigationview
supports action views, quite easy 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 in drawer menu xml, i.e.
menu/drawer.xml
:<item ... app:actionlayout="@layout/menu_counter" />
note should use app
namespace, don't try use android
.
alternatively can manually set action view menuitem.setactionview()
method.
find menu item , 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, need use menuitemcompat if have support android 2.x versions.
Comments
Post a Comment