ListView's ContextActions - how to force display all MenuItems as buttons with icons, not text

chetszotchetszot PLMember ✭✭

Hello

I have ListView with 4 ContextActions attached. On Android, when I long press ListView's item, I see 3 of my MenuItems displayed with icon and the fourth one is displayed as text under "3 dots more" button.

I checked Xamarin.Forms source code and I think that ContextAction's menu items are rendered as regular Android's MenuItems:
https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/CellAdapter.cs

I also checked Android's documentation which says Android can display up to 6 Menu Items:
https://developer.android.com/guide/topics/ui/menus.html#options-menu

Is it possible to force all 4 Menu Items be displayed with icon? It's a must-have for my app to display all 4 Menu Items as icons, no "more" button.

Many thanks for any help
Best regards

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    edited April 1

    In xamarin android , we can notice the following attribute:

      android:showAsAction="always" 
    

    When we set the value to always , it will display the item.
    Meanwhile, when we click into IMenuItem, we can see the following method:

        //
        // Summary:
        //     Sets how this item should display in the presence of an Action Bar.
        //
        // Parameters:
        //   actionEnum:
        //     How the item should display. One of Android.Views.IMenuItem.SHOW_AS_ACTION_ALWAYS,
        //     Android.Views.IMenuItem.SHOW_AS_ACTION_IF_ROOM, or Android.Views.IMenuItem.SHOW_AS_ACTION_NEVER.
        //     SHOW_AS_ACTION_NEVER is the default.
        //
        // Remarks:
        //     Sets how this item should display in the presence of an Action Bar. The parameter
        //     actionEnum is a flag set. One of Android.Views.IMenuItem.SHOW_AS_ACTION_ALWAYS,
        //     Android.Views.IMenuItem.SHOW_AS_ACTION_IF_ROOM, or Android.Views.IMenuItem.SHOW_AS_ACTION_NEVER
        //     should be used, and you may optionally OR the value with Android.Views.IMenuItem.SHOW_AS_ACTION_WITH_TEXT.
        //     SHOW_AS_ACTION_WITH_TEXT requests that when the item is shown as an action, it
        //     should be shown with a text label.
        //     [Android Documentation]
        [Register("setShowAsAction", "(I)V", "GetSetShowAsAction_IHandler:Android.Views.IMenuItemInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", ApiSince = 11)]
        void SetShowAsAction([GeneratedEnum] ShowAsAction actionEnum);
    

    So we can also use like this

     IMenuItem item = menu.Add(global::Android.Views.Menu.None, i,global::Android.Views.Menu.None, action.Text);
     item.SetShowAsAction(ShowAsAction.Always);
    

    The following link should be helpful for you.
    https://github.com/xamarin/monodroid-samples/blob/master/ActionBarSherlock/ActionBarSherlockTest/ActionModes.cs
    https://developer.xamarin.com/api/member/Android.Views.IMenuItem.SetShowAsAction/p/Android.Views.ShowAsAction/
    https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/tool-bar/adding-a-second-toolbar

  • chetszotchetszot PLMember ✭✭

    Hello

    Thank you for your response. I understand how to proceed and where to look for. However - as my codebase is Xamarin.Forms, IMenuItem logic you provided is deepely encapsulated in CellAdapter class, which is a private member of ListViewRenderer.

    Do you have an idea of how to access IMenuItems from Xamarin.Forms's custom renderer? What should I override or access in order to change existing IMenuItem collection?

    Thank you for your support
    Best regards

  • chetszotchetszot PLMember ✭✭

    Hello

    Thank you for your response. I understand how to proceed and where to look for. However - as my codebase is Xamarin.Forms, IMenuItem logic you provided is deepely encapsulated in CellAdapter class, which is a private member of ListViewRenderer.

    Do you have an idea of how to access IMenuItems from Xamarin.Forms's custom renderer? What should I override or access in order to change existing IMenuItem collection?

    Thank you for your support
    Best regards

Sign In or Register to comment.