I use NavigationDrawer
from desin lib
compile 'com.android.support:design:24.0.0'
There is XML
....
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main_second"
app:menu="@menu/activity_third_drawer" />
....
There is point where i am setting menu
app:menu="@menu/activity_third_drawer"
There is XML
of menu
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_inbox"
android:icon="@drawable/ic_mail_outline_white"
android:title="@string/inbox" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_close_24dp"
android:title="@string/gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_close_24dp"
android:title="@string/slideshow" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_close_24dp"
android:title="@string/tools" />
</group>
<item android:title="@string/about_app">
<menu>
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_close_24dp"
android:title="@string/about1" />
</menu>
</item>
</menu>
Eventually it is looks like this
but I need when message is caming to inbox it should be marked in NavigationDrawer
.
Like this
There are a lot of deffirent libs that can help with this issue, I chose this one.
This lib can attach badge wherever you want, but there are no one sample hoe to attach it to <item>
. The closest sample is how to attach to XML
<com.readystatesoftware.viewbadger.BadgeView
android:id="@+id/badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="OK" />
but i still can't think out how to attach it to <item>
Maybe there are another solution how to make it?
I have tryed this way
View menuItemView = MenuItemCompat.getActionView(navigationView.getMenu().findItem(R.id.nav_inbox));
BadgeView badge = new BadgeView(context, menuItemView);
badge.setText("1");
badge.show();
but every time I get menuItemView = null
then I have tried this way:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
View menuItemView = MenuItemCompat.getActionView(menu.findItem(R.id.nav_inbox));
BadgeView badge = new BadgeView(context, menuItemView);
badge.setText("1");
badge.show();
return super.onPrepareOptionsMenu(menu);
}
but it is the same menuItemView = null
:
Debag
What I am doing wrong?
EDIT
My NavigationView
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main_second"
app:menu="@menu/activity_third_drawer" />
My menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_inbox"
android:icon="@drawable/ic_inbox_checked"
android:title="Inbox"
app:actionLayout="@layout/badge" />
</group>
<item android:title="@string/about_app">
<menu>
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_close_24dp"
android:title="@string/about1" />
</menu>
</item>
</menu>
My badge
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textMenuItemCount"
android:layout_width="wrap_content"
android:text="rrrr"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:textColor="@android:color/holo_blue_light"
android:textStyle="bold"
android:textSize="16sp"
android:alpha="0.6"/>
</RelativeLayout>
This is my code
@Override
protected void onResume() {
super.onResume();
NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
MenuItem item = navView.getMenu().findItem(R.id.nav_inbox);
MenuItemCompat.setActionView(item, R.layout.badge);
RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(item);
TextView tv = (TextView) notifCount.findViewById(R.id.textMenuItemCount);
String count = "4";
if (count != null) {
tv.setText(count);
}else{
tv.setText("");
item.setEnabled(false);
}
}
This is screenshot debag
This is what I get finally
it is nothing, but according debug mode all is ok, without any error, but I can't see any badge.
What I am doing wrong?