Example #1
0
        internal static void UpdateMenuItem(AToolbar toolbar,
                                            ToolbarItem item,
                                            int?menuItemIndex,
                                            Context context,
                                            Color tintColor,
                                            PropertyChangedEventHandler toolbarItemChanged,
                                            List <IMenuItem> menuItemsCreated,
                                            List <ToolbarItem> toolbarItemsCreated,
                                            Action <Context, IMenuItem, ToolbarItem> updateMenuItemIcon = null)
        {
            IMenu menu = toolbar.Menu;

            item.PropertyChanged -= toolbarItemChanged;
            item.PropertyChanged += toolbarItemChanged;

            IMenuItem menuitem;

            Java.Lang.ICharSequence newTitle = null;

            if (!String.IsNullOrWhiteSpace(item.Text))
            {
                if (item.Order != ToolbarItemOrder.Secondary && tintColor != null && tintColor != null)
                {
                    var             color       = item.IsEnabled ? tintColor.ToAndroid() : tintColor.MultiplyAlpha(0.302f).ToAndroid();
                    SpannableString titleTinted = new SpannableString(item.Text);
                    titleTinted.SetSpan(new ForegroundColorSpan(color), 0, titleTinted.Length(), 0);
                    newTitle = titleTinted;
                }
                else
                {
                    newTitle = new Java.Lang.String(item.Text);
                }
            }
            else
            {
                newTitle = new Java.Lang.String();
            }

            if (menuItemIndex == null)
            {
                menuitem = menu.Add(0, AppCompat.Platform.GenerateViewId(), 0, newTitle);
                menuItemsCreated?.Add(menuitem);
                toolbarItemsCreated?.Add(item);
            }
            else
            {
                if (menuItemsCreated == null || menuItemsCreated.Count < menuItemIndex.Value)
                {
                    return;
                }

                menuitem = menuItemsCreated[menuItemIndex.Value];

                if (!menuitem.IsAlive())
                {
                    return;
                }

                menuitem.SetTitle(newTitle);
            }

            menuitem.SetEnabled(item.IsEnabled);
            menuitem.SetTitleOrContentDescription(item);

            if (updateMenuItemIcon != null)
            {
                updateMenuItemIcon(context, menuitem, item);
            }
            else
            {
                UpdateMenuItemIcon(context, menuitem, item, tintColor);
            }

            if (item.Order != ToolbarItemOrder.Secondary)
            {
                menuitem.SetShowAsAction(ShowAsAction.Always);
            }

            menuitem.SetOnMenuItemClickListener(new GenericMenuClickListener(((IMenuItemController)item).Activate));

            if (item.Order != ToolbarItemOrder.Secondary && !Forms.IsOreoOrNewer && (tintColor != null && tintColor != null))
            {
                var view = toolbar.FindViewById(menuitem.ItemId);
                if (view is ATextView textView)
                {
                    if (item.IsEnabled)
                    {
                        textView.SetTextColor(tintColor.ToAndroid());
                    }
                    else
                    {
                        textView.SetTextColor(tintColor.MultiplyAlpha(0.302f).ToAndroid());
                    }
                }
            }
        }