I am developing an Android application that contains a dynamically generated menu, so the menu is generated by programming:
private void cargarAvisosMenu()
{
// Menu
final Menu menu = this.mNavigationView.getMenu();
// Carga de datos
CentroSelection selection = new CentroSelection();
CentroCursor cur = selection.query(getApplicationContext().getContentResolver());
// Recorro el cursor de centros
while (cur.moveToNext()) {
boolean avisosTipoA = false;
boolean avisosTipoB = false;
Centro c = Centro.getFromCursor(cur);
if (c.getIsTipoAVisible() && c.getComponenteTipoA().getControlesTipoAPendientes() > 0) {
avisosTipoA = true;
}
if (c.getIsTipoBVisible() && c.getComponenteTipoB().getControlesTipoBPendientes() > 0){
avisosTipoB = true;
}
if (avisosTipoA || avisosTipoB) {
// Añado la sección y los items.
final SubMenu subMenu = menu.addSubMenu(c.getNombre());
if (avisosTipoA) {
subMenu.add("TipoA").setIcon(R.drawable.ic_tipoA);
}
if (avisosTipoB) {
subMenu.add("TipoB").setIcon(R.drawable.ic_tipoB);
}
}
}
}
My intention is to add a counter indicating the number of notifications (gmail style).
According to the documentation this is possible, either through an xml menu, using the attribute app:actionLayout
, or using the function MenuItemCompat.setActionView()
. Since the menu that I am developing is dynamic, I have opted for the second option, so that the code would be as follows:
private void cargarAvisosMenu()
{
// Menu
final Menu menu = this.mNavigationView.getMenu();
// Carga de datos
CentroSelection selection = new CentroSelection();
CentroCursor cur = selection.query(getApplicationContext().getContentResolver());
// Recorro el cursor de centros
while (cur.moveToNext()) {
boolean avisosTipoA = false;
boolean avisosTipoB = false;
Centro c = Centro.getFromCursor(cur);
if (c.getIsTipoAVisible() && c.getComponenteTipoA().getControlesTipoAPendientes() > 0) {
avisosTipoA = true;
}
if (c.getIsTipoBVisible() && c.getComponenteTipoB().getControlesTipoBPendientes() > 0){
avisosTipoB = true;
}
if (avisosTipoA || avisosTipoB) {
// Añado la sección y los items.
final SubMenu subMenu = menu.addSubMenu(c.getNombre());
if (avisosTipoA) {
subMenu.add("TipoA").setIcon(R.drawable.ic_tipoA);
}
if (avisosTipoB) {
// subMenu.add("TipoB").setIcon(R.drawable.ic_tipoB);
int itemId = subMenu.add("TipoB").getItemId();
View menuItem = MenuItemCompat.setActionView(subMenu.findItem(itemId), R.layout.menu_notificaciones).getActionView();
((ImageView) menuItem.findViewById(R.id.ivMenuNotificaciones)).setBackground(getDrawable(R.drawable.ic_barcode));
((TextView) menuItem.findViewById(R.id.tvMenuNotificaciones)).setText("TipoB");
((TextView) menuItem.findViewById(R.id.tvContadorNotificaciones)).setText("100");
}
}
}
}
And the "menu_notifications.xml" file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/ivMenuNotificaciones"
android:layout_width="64dp"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tvMenuNotificaciones"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tvContadorNotificaciones"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"/>
</LinearLayout>
However the menu does not load the "menu_notifications" view.
Finally I have solved the problem by creating the menu manually, introducing a
ListView
inside the componentNavigationView
.If I'm not mistaken, this is the way that was used in the past for the development of the menus. I am aware that this is not the solution you were looking for, however I cannot find a way to add the counter by code directly to the control
NavigationView
.According to
setActionView
the action will be replaced when this item is displayed as an action within the parent.You need to indicate this action. You can do this using
setShowAsAction
. This describes when this item can be displayed.