NavigationView: custom item layout with app:actionLayout how to?

Rino picture Rino · Nov 13, 2015 · Viewed 8.1k times · Source

according to recent Ian Lake post

And if you haven't looked at NavigationView recently, it actually got quite an update in version 23.1.0 [1] with the addition of app:actionLayout support for custom view support.

How can I use app:actionLayout in NavigationView?

Using

<group android:checkableBehavior="single">
    <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_test_1"
        />
    <item
        android:id="@+id/navigation_drawer_item_2"
        android:icon="@drawable/ic_menu_2"
        android:title="@string/navigation_drawer_item_2"
        app:actionLayout="@layout/menu_test_1"
        />
</group>

with

<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"
    app:menu="@menu/activity_main_drawer">

It doesn't work. It shows the actionLayout on the right side of the item. Also removing android:icon and android:title seems not working.

How can i fix it?

Thank you Regards

Answer

Alexandr Shutko picture Alexandr Shutko · Feb 7, 2016

I beat this problem by removing any text from android:title.

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"
    app:menu="@menu/activity_main_drawer" />

Menu code (activity_main_drawer):

<?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_item1"
            app:showAsAction="always"
            android:title=""
            app:actionLayout="@layout/menu_item_layout" />
    </group>
</menu>

menu_item_layout:

<?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/item_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Item text" />
</RelativeLayout>