Creating a pop-up menu in the action context bar

I have an application with a NoActionBar theme. In my main activity, I have an options menu that I created manually at the top of the screen (or using the device’s built-in settings button).

In this main action, I have a snippet with listView , where I apply the function of the long-click action to show the context action panel (CAB) for further user options.

Now I’m trying to add an item to my CAB so that it contains some parameters, such as selecting all the items in the ListView, but since this is a CAB item, I can’t show the pop-up menu as in regular Events. Moreover, I want all parameter menu callbacks (e.g. onOptionsItemSelected) to remain in the CAB context so that I can continue to perform actions in the CAB.

Here is the code of my CAB:

<?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:id="@+id/action_delete" android:orderInCategory="100" app:showAsAction="always" android:icon="@drawable/ic_action_delete" android:title="Delete"/> <item android:id="@+id/action_overflow" app:showAsAction="always" android:orderInCategory="200" android:icon="@drawable/ic_action_overflow" android:title="Options" android:visible="false"/> </menu> 
+5
source share
1 answer

Apparently, I missed the built-in CAB function - the built-in overflow menu that destroys some action items when the screen is too small to show all of them.

Another manipulation that must be performed in order to always minimize certain actions in this overflow menu is to set for each of them:

 android:showAsAction="never" app:showAsAction="never" 

So, let's say we have 3 actions (delete, selece_all, add) in the CAB, and we want two of them (select_all, add) to be always minimized in the built-in overflow menu, we will set this to CAB xml:

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:mm="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_delete" android:orderInCategory="100" mm:showAsAction="always" android:icon="@drawable/ic_action_delete" android:title="Delete"/> <item android:id="@+id/action_select" android:orderInCategory="200" android:showAsAction="never" mm:showAsAction="never" android:title="@string/select_all"/> <item android:id="@+id/action_add" android:orderInCategory="300" android:showAsAction="never" mm:showAsAction="never" android:title="@string/button_add"/> 

+2
source

Source: https://habr.com/ru/post/1210105/


All Articles