5

I have a menu in the title bar of my Android app, that is not a pop-up Menu. In it I have some items. I want to add a line, or a separator, between just one pair of items in the list. I don't want dividers between all the items, just one pair. I tryed with groups who have different IDs, not worked, also tryed with android:actionlayout, no succes.

My current menu looks like this in design mode. I want to do something like this.

My XML containing my menu:

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

    <item android:title="@string/editare_nume_jucatori">
        <!-- submeniul meu -->
        <menu>
            <item
                android:id="@+id/M_Jucator1"
                android:enabled="true"
                android:title="@string/Jucatorul1" />

            <item
                android:id="@+id/M_Jucator2"
                android:enabled="true"
                android:title="@string/Jucatorul2" />
        </menu>
    </item>

    <item
        android:id="@+id/M_Detalii"
        android:icon="@drawable/dice10"
        android:title="@string/detalii_text_meniu" />

    <item
        android:id="@+id/M_Despre_Aplicatie"
        android:icon="@drawable/dice10"
        android:title="@string/despre_aplicatie" />

    <item
        android:id="@+id/M_Iesire_Aplicatie"
        android:icon="@drawable/m3"
        android:title="@string/IesireAplicatie" />


</menu>

My Java code for the menu:

Menu meniu1;  //a variable used in my menu

//to show my menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.meniul_meu, menu);
    meniu1 = menu; //this is my variable from up declaration
    return true;
}

//here execute different actions for items clicked in menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {

        //click on my item ID from menu and execute
        case R.id.M_Jucator1:

            ...(code code)...

            return true;

        //click on my item ID from menu and execute
        case R.id.M_Jucator2:

            ..(code code)...

            return true;

        //click on my item ID from menu and execute
        case R.id.M_Detalii:

           ..(code code)...

            return true;

        //cand dai click pe iesire din meniu
        case R.id.M_Iesire_Aplicatie:

            ..(code code)..

            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
} //finish meniu codes
Cristian Babarusi
  • 1,039
  • 10
  • 16
  • 1
    Possible duplicate of [How to add dividers between specific menu items?](http://stackoverflow.com/questions/33277124/how-to-add-dividers-between-specific-menu-items) – ʍѳђઽ૯ท Jan 01 '17 at 23:02
  • 1
    is not a duplicate, please... i tryed with menu item group with different IDs for each groups but no divider appeared, also i tryed with android:actionlayout and failed. – Cristian Babarusi Jan 01 '17 at 23:17

1 Answers1

0

Group your items in the XML menu, such as:

......

<group>
    <items...
</group>

<group>
    <items...
</group>

.....

And the in your code use:

final Menu menu = ((Toolbar)this.findViewById(R.id.your_toolbar)).getMenu();
MenuCompat.setGroupDividerEnabled(menu, true);
PerracoLabs
  • 12,362
  • 12
  • 57
  • 107