Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 350
android developer Avatar asked Oct 22 '15 08:10

android developer


People also ask

How do I add a separator to my WordPress menu?

Adding WordPress Menu Separator In the Custom links block add a new menu item with the same name. Add # hash symbol. Add # hash symbol into the link field to create a non-clickable menu item. Click on Add to Menu button to add the non-clickable item (separator) to your existing menu.

How do I create a menu and menu group?

Way to create menu directory and menu resource file: To create the menu directory just right-click on res folder and navigate to res->New->Android Resource Directory. Give resource directory name as menu and resource type also menu. one directory will be created under res folder.


1 Answers

You should use action layout

<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=".LandingActivity">     <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 then the action layout can have the textview with divider.

<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> 

then you can add the click listener in code

like image 110
shreyas Avatar answered Oct 01 '22 07:10

shreyas