Search code examples
androidnavigation-drawermenuitem

How to set ellipsize in menu item of NavigationView?


I want set android:ellipsize="end" in menu items of NavigationView. In my current implementaion when text in menu item is too long, it's just cut at the end. Here is what I've tried so far:

<android.support.design.widget.NavigationView
        app:itemTextAppearance="@style/AppTheme.Widget.NavigationView.TextAppearance"
        android:id="@+id/navigation"
        style="@style/AppTheme.Widget.NavigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/drawer_view"/>

<style name="AppTheme.Widget.NavigationView" parent="Widget.Design.NavigationView">
        <item name="android:textAppearance">@style/AppTheme.Widget.NavigationView.TextAppearance</item>
        <item name="android:background">@color/drawer_background</item>
        <item name="itemTextColor">@color/color_drawer_item</item>
        <item name="itemIconTint">@color/color_drawer_item</item>
</style>

<style name="AppTheme.Widget.NavigationView.TextAppearance">
        <item name="android:ellipsize">end</item>
        <item name="android:maxLines">1</item>
</style>

Edit:

Another possible solution is described here: Android NavigationView : not show full item and not truncate


Solution

  • Add a new style to the styles.xml:

    res / values / styles.xml

    <style name="TextAppearance">
        <item name="android:ellipsize">end</item>
    </style>
    

    Set the new style to the NavigationView:

    res / layout / activity_main.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"
        app:menu="@menu/activity_main_drawer"
        app:theme="@style/TextAppearance" />