Aleksander Mielczarek Aleksander Mielczarek - 2 months ago 30
Android Question

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

Answer

Set the text appearance as a theme:

res / layout / activity_main.xml

<android.support.design.widget.NavigationView
    app:theme="@style/ItemTextAppearance"
    android:background="@color/drawer_background"
    app:itemTextColor="@color/color_drawer_item"
    app:itemIconTint="@color/color_drawer_item"
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/drawer_view" />

res / values / styles.xml

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