androiddrop-down-menumenucontextmenumenuitem

How to add line divider for menu item Android


My menu item become bigger so that I want group them and make a line divider to separate each group. What should I do now ?

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
<!--group1-->
            <item
                android:id="@+id/action_addtag"
                android:title="@string/add_hashtag_string"
                app:showAsAction="never" />
            <item
                android:id="@+id/action_block_list"
                android:title="Block"
                app:showAsAction="never" />
            <item
                android:id="@+id/action_report_list"
                android:title="Report"
                app:showAsAction="never" />
<!--group2-->
            <item
                android:id="@+id/terms"
                android:title="Terms"
                app:showAsAction="never" />
            <item
                android:id="@+id/feedback"
                android:title="FeedBack"
                app:showAsAction="never" />
<!--group3-->
            <item
                android:id="@+id/action_setting"
                android:title="Setting"
                app:showAsAction="never" />
    </menu>

Solution

  • All you need to do is define a group with an unique ID, I have checked the implementation if group has different id's it will create a divider.

    Example menu, creating the separator:

    <menu 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"
    tools:context=".MainActivity">
    
    <group android:id="@+id/grp1">
        <item
            android:id="@+id/navigation_item_1"
            android:checked="true"
            android:icon="@drawable/ic_home"
            android:title="@string/navigation_item_1" />
    </group>
    
    <group android:id="@+id/grp2">
        <item
            android:id="@+id/navigation_item_2"
            android:icon="@drawable/ic_home"
            android:title="@string/navigation_item_2" />
    </group>
    

    hope this helps

    UPDATE

    for menu item may be you can use this

        <menu 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">
        <item
            android:id="@+id/action_cart"
            android:title="cart"
            android:actionLayout="@layout/cart_update_count"
            android:icon="@drawable/shape_notification"
            app:showAsAction="always"/>
    </menu>
    

    and actionLayout file will be

     <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/divider"/>
    
        <TextView
            android:id="@android:id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:gravity="center_vertical"          
            android:textAppearance="?attr/textAppearanceListItemSmall"/>
    
    </LinearLayout>