Search code examples
androidandroid-actionbarsubmenuandroid-toolbardivider

How to add dividers between specific menu items?


Background

I have a menu item in the action bar (toolbar actually) that when clicked, shows a list of items to choose from, similar to radio-buttons:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:icon="@drawable/..."
        android:title="@string/..."
        app:showAsAction="always">
        <menu>
            <group
                android:id="@+id/..."
                android:checkableBehavior="single">
                <item .../>
                <item .../>
                <item .../>
            </group>
        </menu>
    </item>
</menu>

I need to put an item below this list of items, that will have a divider between it and the list. Similar to what the material design guidelines show (taken from here) :

enter image description here

EDIT: here's a sketch of what I want to do:

enter image description here

The problem

I can't find a way to do it.

What I've tried

The only possible solutions I've found are:

  1. change the theme of the activity (here), but this will also affect other menu items of the activity

  2. methods to put a divider between menu items when they appear on the action bar, but here they do not appear on the toolbar itself. They appear on a popup menu of a selected item.

  3. I tried to put fake items between the list and the extra item, and I also tried to put a group, an empty group and even tried various attributes.

Sadly nothing worked.

The question

How can I add a divider between specific items of an action-item's popup menu ?

Perhaps I need to create a custom popup menu when clicking on the action item (like here) ? If so, how do I put a divider between specific items there ? Maybe use a Spinner as an action item?


Solution

  • In Material3 Design, MDC-Android, make menu items in each group, and call setGroupDividerEnabled(Boolean).

    Dividers will inserted between group or menu item(s).

    Menu divider example

    MyActivity.kt

    private val binding by lazy { ActivityMyBinding.inflate(layoutInflater) }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
         
        // other initializations..
        // ...
    
        binding.toolbar.menu.setGroupDividerEnabled(true)
    }
    

    activity_my.xml

    <androidx.coordinatorlayout.widget.CoordinatorLayout 
        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:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <com.google.android.material.appbar.MaterialToolbar
                <!-- Other properties -->
                android:id="@+id/toolbar"
                app:menu="@menu/main" />
    
        </com.google.android.material.appbar.AppBarLayout>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    menu.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <item
            android:id="@+id/item0"
            android:title="item0"
            app:showAsAction="never" />
    
    
        <item
            android:id="@+id/item1"
            android:title="item1"
            app:showAsAction="never" />
    
        <group android:id="@+id/group0">
            <item
                android:id="@+id/item2"
                android:title="group0 item2"
                app:showAsAction="never" />
        </group>
    
        <group android:id="@+id/group1">
            <item
                android:id="@+id/item3"
                android:title="group1 item3"
                app:showAsAction="never" />
    
            <item
                android:id="@+id/item4"
                android:title="group1 item4"
                app:showAsAction="never" />
        </group>
    
    </menu>