Trabajando en mi proyecto Komeers me he topado con un problema a la hora de añadir un contador de notificaciones nuevas en el NavigationView. 

Después de investigar he encontrado una solución bastante rápida y sencilla.

Se necesita crear un Layout Resource File, por ejemplo este:

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" />

Luego, hace falta ir al XML del menu que utilizamos en el Navigation Drawer y añadir el atributo actionLayout y hacer referencia al layout creado anteriormente. 

<item
    android:id="@+id/navigation_drawer_item_1"
    android:icon="@drawable/ic_menu_1"
    android:title="@string/navigation_drawer_item_1"
    app:actionLayout="@layout/menu_counter"
    />

Al final necesitamos, añadir la siguiente función en el activity donde tengamos definido el NavigationView. 

private void setMenuCounter(@IdRes int itemId, int count) {
    TextView view = (TextView) navigationView.getMenu().findItem(itemId).getActionView();
    view.setText(count > 0 ? String.valueOf(count) : null);
}

Os comparto el enlace dónde encontré esta solución: https://stackoverflow.com/questions/32563200/how-to-set-unread-notification-count-in-navigationview-of-drawerlayout/33709256

Si no os funciona, escribirme a ver si os puedo ayudar. O si tenéis otra solución podéis compartirla con el resto.

Un saludo,

 

David

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *