0

I'm developping an app that contains a menu that is generated dynamically:

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);
                }
            }
        }
    }

I want to add a counter to MenuItem that contains a number of notifications (as gmail).

According to this post is posible:

NavigationView provides a convenient way to build a navigation drawer, including the ability to creating menu items using a menu XML file. We’ve expanded the functionality possible with the ability to set custom views for items via app:actionLayout or using MenuItemCompat.setActionView().

As I'm developing a dynamic menu, I tried with the second option MenuItemCompat.setActionView():

 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");
                }
            }
        }
    }

"menu_notificaciones.xml":

<?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>

But it doesn't work.

hecnabae
  • 407
  • 4
  • 21

2 Answers2

0

More important is to use lib version v7:23.1.1

use compile 'com.android.support:appcompat-v7:23.1.1'

Menu items

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single" android:id="@+id/root_group">
    <item android:id="@+id/nav_switch"
        app:actionLayout="@layout/action_view_switch"
        android:title="Downloaded only" />
    <item android:title="You" android:id="@+id/you_parent_item">
        <menu android:id="@+id/you_menu">
            <group android:checkableBehavior="single" android:id="@+id/you_group">
                <item
                    android:id="@+id/item_you_summary"
                    android:title="Summary Detail" />
                <item
                    android:id="@+id/item_calendar"
                    android:title="Calendar" />

            </group>
        </menu >
    </item>
    <item android:title="Your People" android:id="@+id/your_parent_item">
        <menu android:id="@+id/your_menu">
            <group android:checkableBehavior="single" android:id="@+id/your_group">
                <item
                    android:id="@+id/your_item_sumary"
                    android:title="View Summary" />
                <item
                    android:id="@+id/item_record_timeout"
                    android:title="Record TimeOut" />
            </group>
        </menu>
    </item>

</group>

custom views for items via app:actionLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal" android:layout_width="match_parent"

android:layout_height="match_parent">



<android.support.v7.widget.SwitchCompat

    android:layout_width="fill_parent"

    android:layout_height="match_parent"

    android:text="Switch"/>

Akash Jagtap
  • 404
  • 2
  • 8
0

I had the same problem. I solved with this I think you need to replace your "AvisosTipoB" if

You need to get the menu item, later with MenuItemCompat set the action (setActionView), passing menuItem and layout (R.layout.menu_notificaciones). Declare Layout and assign = (xxxxLayout) MenuItemCompat.getActionView(menuItem)

Community
  • 1
  • 1
Abel
  • 1