AnZ
AnZ

Reputation: 1060

Underline menu item in navigation drawer list

How do you underline menu items in navigation drawer?

expectation:

enter image description here

reality:

enter image description here

@menu/activity_navigation_drawer:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_open_account"
        android:title="@string/na_navigate_open_account" />
    <item
        android:id="@+id/nav_login"
        android:title="@string/na_navigate_login" />
    <item
        android:id="@+id/nav_stock_signals"
        android:title="@string/na_navigate_stock_signals" />

    ...

</group>

navigation drawer activity layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end">

<include
    layout="@layout/app_bar_navigation"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    android:background="@color/primary_asphalt_dark"
    app:headerLayout="@layout/nav_header_navigation"
    app:menu="@menu/activity_navigation_drawer" /> 

</android.support.v4.widget.DrawerLayout>

Tell me if some more code needed.

Upvotes: 3

Views: 4746

Answers (2)

NickUnuchek
NickUnuchek

Reputation: 12847

Press twice Shift

Open file design_navigation_menu_item.xml from com.android.support:design-26.1.0

Copy the file design_navigation_menu_item.xml into \app\src\main\res\layout to override it

Add android:paddingStart="@dimen/design_navigation_icon_padding" to CheckedTextView, make the file looks like this:

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

<CheckedTextView
        android:id="@+id/design_menu_item_text"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawablePadding="@dimen/design_navigation_icon_padding"
        android:paddingStart="@dimen/design_navigation_icon_padding"
        android:gravity="center_vertical|start"
        android:maxLines="1"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>

<ViewStub
        android:id="@+id/design_menu_item_action_area_stub"
        android:inflatedId="@+id/design_menu_item_action_area"
        android:layout="@layout/design_menu_item_action_area"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

</merge>

In your activity_main.xml add to your NavigationView app:itemBackground="@drawable/drawer_item_background_selector", it should be like this:

<android.support.design.widget.NavigationView
      ...
        app:itemBackground="@drawable/drawer_item_background_selector"
      .../>

Create drawable/drawer_item_background_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/drawer_item_background_selected" android:state_pressed="true" />
    <item android:drawable="@color/drawer_item_background_selected" android:state_checked="true" />
    <item android:drawable="@color/drawer_item_background_selected" android:state_focused="true" />
    <item android:drawable="@color/drawer_item_background_selected" android:state_activated="true" />
    <item android:drawable="@drawable/drawer_item_background_default" />
</selector>

Create drawable/drawer_item_background_default.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/drawer_transparent" />
    <stroke
        android:width="1dp"
        android:color="@color/appGrayLight" />
    <padding
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp" />
</shape>

enter image description here

Upvotes: 0

Sevle
Sevle

Reputation: 3119

Each group ends with a line separator. Consequently, if each item in your menu has its own group you will achieve the desired graphical output:

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:id="@+id/nav_open_account_group"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_open_account"
            android:title="@string/na_navigate_open_account" />
        </group>
    <group android:id="@+id/nav_login_group"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_login"
            android:title="@string/na_navigate_login" />
    </group>
    <group android:id="@+id/nav_stock_signals_group"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_stock_signals"
            android:title="@string/na_navigate_stock_signals" />
     </group>

        ...

    </menu>

enter image description here

Note that the different ids for each group are required for this to work.

Upvotes: 8

Related Questions