public static void UpdateBackButton(this AToolbar nativeToolbar, Toolbar toolbar) { _defaultNavigationIcon ??= nativeToolbar.NavigationIcon; if (toolbar.BackButtonVisible) { nativeToolbar.NavigationIcon ??= _defaultNavigationIcon; var backButtonTitle = toolbar.BackButtonTitle; ImageSource image = toolbar.TitleIcon; if (!string.IsNullOrEmpty(backButtonTitle)) { nativeToolbar.NavigationContentDescription = backButtonTitle; } else if (image == null || nativeToolbar.SetNavigationContentDescription(image) == null) { nativeToolbar.SetNavigationContentDescription(Resource.String.nav_app_bar_navigate_up_description); } } else { if (!toolbar.DrawerToggleVisible) { nativeToolbar.NavigationIcon = null; } } nativeToolbar.UpdateIconColor(toolbar); nativeToolbar.UpdateBarTextColor(toolbar); }
public static void UpdateBarTextColor(this AToolbar nativeToolbar, Toolbar toolbar) { var textColor = toolbar.BarTextColor; // Because we use the same toolbar across multiple navigation pages (think tabbed page with nested NavigationPage) // We need to reset the toolbar text color to the default color when it's unset if (_defaultTitleTextColor == null) { var a = TintTypedArray.ObtainStyledAttributes(nativeToolbar.Context?.GetThemedContext(), null, Resource.Styleable.Toolbar, Resource.Attribute.toolbarStyle, 0); _defaultTitleTextColor = a.GetColorStateList(Resource.Styleable.Toolbar_titleTextColor); a.Recycle(); } if (textColor != null) { nativeToolbar.SetTitleTextColor(textColor.ToPlatform().ToArgb()); } else { nativeToolbar.SetTitleTextColor(_defaultTitleTextColor); } if (nativeToolbar.NavigationIcon is DrawerArrowDrawable icon) { if (textColor != null) { _defaultNavigationIconColor = icon.Color; icon.Color = textColor.ToPlatform().ToArgb(); } else if (_defaultNavigationIconColor != null) { icon.Color = _defaultNavigationIconColor.Value; } } }
protected virtual void SetColors(AToolbar toolbar, IShellToolbarTracker toolbarTracker, Color foreground, Color background, Color title) { var titleArgb = title.ToAndroid(ShellRenderer.DefaultTitleColor).ToArgb(); if (_titleTextColor != titleArgb) { toolbar.SetTitleTextColor(titleArgb); _titleTextColor = titleArgb; } var newColor = background.ToAndroid(ShellRenderer.DefaultBackgroundColor); if (!(toolbar.Background is ColorDrawable cd) || cd.Color != newColor) { using (var colorDrawable = new ColorDrawable(background.ToAndroid(ShellRenderer.DefaultBackgroundColor))) toolbar.SetBackground(colorDrawable); } var newTintColor = foreground ?? ShellRenderer.DefaultForegroundColor; if (toolbarTracker.TintColor != newTintColor) { toolbarTracker.TintColor = newTintColor; } }
public static void UpdateIsVisible(this AToolbar nativeToolbar, Toolbar toolbar) { _ = nativeToolbar.Context ?? throw new ArgumentNullException(nameof(nativeToolbar.Context)); bool showNavBar = toolbar.IsVisible; var lp = nativeToolbar.LayoutParameters; if (lp == null) { return; } if (!showNavBar) { lp.Height = 0; } else { if (toolbar.BarHeight != null) { lp.Height = (int)nativeToolbar.Context.ToPixels(toolbar.BarHeight.Value); } else { lp.Height = nativeToolbar.Context?.GetActionBarHeight() ?? 0; } } nativeToolbar.LayoutParameters = lp; }
void Destroy() { ((IShellController)_shellContext.Shell).RemoveAppearanceObserver(this); if (_shellContent != null) { ((IShellContentController)_shellContent).RecyclePage(_page); _page.Handler = null; } if (_shellPageContainer != null) { _shellPageContainer.RemoveAllViews(); if (_root is ViewGroup vg) { vg.RemoveView(_shellPageContainer); } _shellPageContainer.Dispose(); } _root?.Dispose(); _toolbarTracker?.Dispose(); _appearanceTracker?.Dispose(); _appearanceTracker = null; _toolbarTracker = null; _toolbar = null; _root = null; _viewhandler = null; _shellContent = null; _shellPageContainer = null; }
private void SetupToolbar(Toolbar toolbar) { toolbar.Title = $"{(_completeMedia ? "Completing" : (_mediaList == null ? "Adding" : "Editing"))}: {_media.Title.UserPreferred}"; toolbar.InflateMenu(Resource.Menu.EditMediaListItem_ActionBar); var privateItem = toolbar.Menu.FindItem(Resource.Id.Menu_EditMediaListItem_MarkPrivate); SetupIsPrivate(privateItem); var priorityItem = toolbar.Menu.FindItem(Resource.Id.Menu_EditMediaListItem_MarkPriority); SetupPriority(priorityItem); toolbar.MenuItemClick += async(sender, args) => { if (args.Item.ItemId == Resource.Id.Menu_EditMediaListItem_Save) { await SaveMediaListItem(args.Item); } else if (args.Item.ItemId == Resource.Id.Menu_EditMediaListItem_MarkPrivate) { _isPrivate = !_isPrivate; SetupIsPrivate(privateItem); } else if (args.Item.ItemId == Resource.Id.Menu_EditMediaListItem_MarkPriority) { _priority = _priority > 0 ? 0 : 1; SetupPriority(priorityItem); } }; }
void Destroy() { ((IShellController)_shellContext.Shell).RemoveAppearanceObserver(this); if (_shellContent != null) { ((IShellContentController)_shellContent).RecyclePage(_page); _page.ClearValue(Platform.RendererProperty); _page = null; } if (_shellPageContainer != null) { _shellPageContainer.RemoveAllViews(); if (_root is ViewGroup vg) { vg.RemoveView(_shellPageContainer); } } _renderer?.Dispose(); _root?.Dispose(); _toolbarTracker?.Dispose(); _appearanceTracker?.Dispose(); _appearanceTracker = null; _toolbarTracker = null; _toolbar = null; _root = null; _renderer = null; _shellContent = null; }
public static void UpdateBarBackground(this AToolbar nativeToolbar, Toolbar toolbar) { Brush barBackground = toolbar.BarBackground; if (barBackground is SolidColorBrush solidColor) { var tintColor = solidColor.Color; if (tintColor == null) { nativeToolbar.BackgroundTintMode = null; } else { nativeToolbar.BackgroundTintMode = PorterDuff.Mode.Src; nativeToolbar.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToPlatform()); } } else { nativeToolbar.UpdateBackground(barBackground); if (Brush.IsNullOrEmpty(barBackground)) { nativeToolbar.BackgroundTintMode = null; } } }
void Destroy() { if (_rootView != null) { UnhookEvents(); //_viewPager.RemoveOnPageChangeListener(this); var adapter = _viewPager.Adapter; _viewPager.Adapter = null; adapter.Dispose(); _tablayout.LayoutChange -= OnTabLayoutChange; _toolbarAppearanceTracker.Dispose(); _tabLayoutAppearanceTracker.Dispose(); _toolbarTracker.Dispose(); _tablayout.Dispose(); _toolbar.Dispose(); _viewPager.Dispose(); _rootView.Dispose(); } _toolbarAppearanceTracker = null; _tabLayoutAppearanceTracker = null; _toolbarTracker = null; _tablayout = null; _toolbar = null; _viewPager = null; _rootView = null; }
public static void UpdateTitleIcon(this AToolbar nativeToolbar, Toolbar toolbar) { _ = nativeToolbar.Context ?? throw new ArgumentNullException(nameof(nativeToolbar.Context)); _ = toolbar?.Handler?.MauiContext ?? throw new ArgumentNullException(nameof(toolbar.Handler.MauiContext)); ImageSource source = toolbar.TitleIcon; if (source == null || source.IsEmpty) { if (nativeToolbar.GetChildAt(0) is ToolbarTitleIconImageView existingImageView) { nativeToolbar.RemoveView(existingImageView); } return; } var iconView = new ToolbarTitleIconImageView(nativeToolbar.Context); nativeToolbar.AddView(iconView, 0); iconView.SetImageResource(global::Android.Resource.Color.Transparent); source.LoadImage(toolbar.Handler.MauiContext, (result) => { iconView.SetImageDrawable(result?.Value); AutomationPropertiesProvider.AccessibilitySettingsChanged(iconView, source); }); }
public virtual void SetAppearance(AToolbar toolbar, IShellToolbarTracker toolbarTracker, ShellAppearance appearance) { var foreground = appearance.ForegroundColor; var background = appearance.BackgroundColor; var titleColor = appearance.TitleColor; SetColors(toolbar, toolbarTracker, foreground, background, titleColor); }
public static void UpdateIconColor(this AToolbar nativeToolbar, Toolbar toolbar) { var navIconColor = toolbar.IconColor; if (navIconColor != null && nativeToolbar.NavigationIcon != null) { nativeToolbar.NavigationIcon.SetColorFilter(navIconColor, FilterMode.SrcAtop); } }
public static void DisposeMenuItems(this AToolbar toolbar, IEnumerable <ToolbarItem> toolbarItems, PropertyChangedEventHandler toolbarItemChanged) { if (toolbarItems == null) { return; } foreach (var item in toolbarItems) { item.PropertyChanged -= toolbarItemChanged; } }
public ShellToolbarTracker(IShellContext shellContext, Toolbar toolbar, DrawerLayout drawerLayout) { ShellContext = shellContext ?? throw new ArgumentNullException(nameof(shellContext)); _toolbar = toolbar ?? throw new ArgumentNullException(nameof(toolbar)); _drawerLayout = drawerLayout ?? throw new ArgumentNullException(nameof(drawerLayout)); _appBar = _toolbar.Parent.GetParentOfType <AppBarLayout>(); _globalLayoutListener = new GenericGlobalLayoutListener(() => UpdateNavBarHasShadow(Page)); _appBar.ViewTreeObserver.AddOnGlobalLayoutListener(_globalLayoutListener); _toolbar.SetNavigationOnClickListener(this); ((IShellController)ShellContext.Shell).AddFlyoutBehaviorObserver(this); }
public static void UpdateBarBackgroundColor(this AToolbar nativeToolbar, Toolbar toolbar) { var tintColor = toolbar.BarBackgroundColor; if (tintColor == null) { nativeToolbar.BackgroundTintMode = null; } else { nativeToolbar.BackgroundTintMode = PorterDuff.Mode.Src; nativeToolbar.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToPlatform()); } }
public static void UpdateIconColor(this AToolbar nativeToolbar, Toolbar toolbar) { var navIconColor = toolbar.IconColor; if (navIconColor != null && nativeToolbar.NavigationIcon != null) { if (nativeToolbar.NavigationIcon is DrawerArrowDrawable dad) { dad.Color = Android.Graphics.Color.White; } nativeToolbar.NavigationIcon.SetColorFilter(navIconColor, FilterMode.SrcAtop); } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); SetContentView(Resource.Layout.activity_main); Toolbar toolbar = FindViewById <Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); SetSupportActionBar(toolbar); var showDialogButton = FindViewById <Button>(Resource.Id.showDialogButton); showDialogButton.Click += ShowDialogButton_Click; }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { var view = Activity.LayoutInflater.Inflate(Resource.Layout.Fragment_BrowseFilterDialog, container, false); SetupToolbar(_toolbar = view.FindViewById <Toolbar>(Resource.Id.BrowseFilterDialog_Toolbar)); SetupTypeSpinner(_typeSpinner = view.FindViewById <AppCompatSpinner>(Resource.Id.BrowseFilterDialog_TypeSpinner), _browseModel.Type); SetupFormatSpinner( _formatSpinner = view.FindViewById <AppCompatSpinner>(Resource.Id.BrowseFilterDialog_FormatSpinner), _browseModel.Type, _browseModel.Format); SetupStatusSpinner( _statusSpinner = view.FindViewById <AppCompatSpinner>(Resource.Id.BrowseFilterDialog_StatusSpinner), _browseModel.Status); SetupCountrySpinner( _countrySpinner = view.FindViewById <AppCompatSpinner>(Resource.Id.BrowseFilterDialog_CountrySpinner), _browseModel.Country); SetupSourceSpinner( _sourceSpinner = view.FindViewById <AppCompatSpinner>(Resource.Id.BrowseFilterDialog_SourceSpinner), _browseModel.Source); SetupSeasonSpinner( _seasonSpinner = view.FindViewById <AppCompatSpinner>(Resource.Id.BrowseFilterDialog_SeasonSpinner), _browseModel.Type, _browseModel.Season); SetupYearPicker(_yearPicker = view.FindViewById <Picker>(Resource.Id.BrowseFilterDialog_YearPicker), _browseModel.Year); _selectedGenres = _browseModel.IncludedGenres; SetupGenresButton(_genresButton = view.FindViewById <AppCompatButton>(Resource.Id.BrowseFilterDialog_GenresButton)); _selectedTags = _browseModel.IncludedTags; SetupTagsButton(_tagsButton = view.FindViewById <AppCompatButton>(Resource.Id.BrowseFilterDialog_TagsButton)); _selectedStreamingOn = _browseModel.LicensedBy; SetupStreamingOnButton(_streamingOnButton = view.FindViewById <AppCompatButton>(Resource.Id.BrowseFilterDialog_StreamingOnButton)); return(view); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); SetContentView(Resource.Layout.activity_main); AndroidX.AppCompat.Widget.Toolbar toolbar = FindViewById <AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); Button btnRequestPermissions = FindViewById <Button>(Resource.Id.btnRequestPermission); btnRequestPermissions.Click += btnRequestPermissionClick; Button btnGetDeviceIdentifiers = FindViewById <Button>(Resource.Id.btnGetDeviceIdentifiers); btnGetDeviceIdentifiers.Click += btnGetDeviceIdentifiersClick; }
private void SetupToolbar(Toolbar toolbar) { toolbar.Title = "Browse Filters"; toolbar.InflateMenu(Resource.Menu.BrowseFilters_ActionBar); toolbar.MenuItemClick += (sender, args) => { if (args.Item.ItemId == Resource.Id.Menu_BrowseFilters_Apply) { ApplyFilters(); } else if (args.Item.ItemId == Resource.Id.Menu_BrowseFilters_Reset) { ResetFilters(); } }; }
public override AView OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (_shellContent != null) { _page = ((IShellContentController)_shellContent).GetOrCreateContent(); } _root = inflater.Inflate(Controls.Resource.Layout.shellcontent, null).JavaCast <CoordinatorLayout>(); var shellContentMauiContext = MauiContext.MakeScoped(layoutInflater: inflater, fragmentManager: ChildFragmentManager); Maui.IElement parentElement = (_shellContent as Maui.IElement) ?? _page; var shellToolbar = new Toolbar(parentElement); ShellToolbarTracker.ApplyToolbarChanges(_shellContext.Shell.Toolbar, shellToolbar); _toolbar = (AToolbar)shellToolbar.ToPlatform(shellContentMauiContext); var appBar = _root.FindViewById <AppBarLayout>(Resource.Id.shellcontent_appbar); appBar.AddView(_toolbar); _viewhandler = _page.ToHandler(shellContentMauiContext); _shellPageContainer = new ShellPageContainer(Context, _viewhandler); if (_root is ViewGroup vg) { vg.AddView(_shellPageContainer); } _toolbarTracker = _shellContext.CreateTrackerForToolbar(_toolbar); _toolbarTracker.SetToolbar(shellToolbar); _toolbarTracker.Page = _page; // this is probably not the most ideal way to do that _toolbarTracker.CanNavigateBack = _shellContent == null; _appearanceTracker = _shellContext.CreateToolbarAppearanceTracker(); ((IShellController)_shellContext.Shell).AddAppearanceObserver(this, _page); if (_shellPageContainer.LayoutParameters is CoordinatorLayout.LayoutParams layoutParams) { layoutParams.Behavior = new AppBarLayout.ScrollingViewBehavior(); } return(_root); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); SetContentView(Resource.Layout.activity_main); inputBill = FindViewById <EditText>(Resource.Id.inputBill); outputTip = FindViewById <TextView>(Resource.Id.outputTip); outputTotal = FindViewById <TextView>(Resource.Id.outputTotal); calculateButton = FindViewById <Button>(Resource.Id.calculateButton); calculateButton.Click += OnCalculateClick; AndroidX.AppCompat.Widget.Toolbar toolbar = FindViewById <AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); }
public static void UpdateMenuItems(this AToolbar toolbar, IEnumerable <ToolbarItem> sortedToolbarItems, IMauiContext mauiContext, Color?tintColor, PropertyChangedEventHandler toolbarItemChanged, List <IMenuItem> previousMenuItems, List <ToolbarItem> previousToolBarItems, Action <Context, IMenuItem, ToolbarItem>?updateMenuItemIcon = null) { if (sortedToolbarItems == null || previousMenuItems == null) { return; } var context = mauiContext.Context; var menu = toolbar.Menu; foreach (var toolbarItem in previousToolBarItems) { toolbarItem.PropertyChanged -= toolbarItemChanged; } int i = 0; foreach (var item in sortedToolbarItems) { UpdateMenuItem(toolbar, item, i, mauiContext, tintColor, toolbarItemChanged, previousMenuItems, previousToolBarItems, updateMenuItemIcon); i++; } int toolBarItemCount = i; while (toolBarItemCount < previousMenuItems.Count) { if (menu != null) { menu.RemoveItem(previousMenuItems[toolBarItemCount].ItemId); } previousMenuItems[toolBarItemCount].Dispose(); previousMenuItems.RemoveAt(toolBarItemCount); } previousToolBarItems.Clear(); previousToolBarItems.AddRange(sortedToolbarItems); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); SetContentView(Resource.Layout.activity_main); AndroidX.AppCompat.Widget.Toolbar toolbar = FindViewById <AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); AppCompatButton fab = FindViewById <AppCompatButton>(Resource.Id.button1); fab.Click += FabOnClick; OrientationSensor.ReadingChanged += OrientationSensor_ReadingChanged; Battery.BatteryInfoChanged += Battery_BatteryInfoChanged; MulticastListener(); }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { _view = Activity.LayoutInflater.Inflate(Resource.Layout.Fragment_MediaListFilterDialog, container, false); SetupToolbar(_toolbar = _view.FindViewById <Toolbar>(Resource.Id.MediaListFilterDialog_Toolbar)); SetupFormatSpinner( _formatSpinner = _view.FindViewById <AppCompatSpinner>(Resource.Id.MediaListFilterDialog_FormatSpinner), _type, _mediaListFilterModel.Format); SetupStatusSpinner( _statusSpinner = _view.FindViewById <AppCompatSpinner>(Resource.Id.MediaListFilterDialog_StatusSpinner), _mediaListFilterModel.Status); SetupSourceSpinner( _sourceSpinner = _view.FindViewById <AppCompatSpinner>(Resource.Id.MediaListFilterDialog_SourceSpinner), _mediaListFilterModel.Source); SetupSeasonSpinner( _seasonSpinner = _view.FindViewById <AppCompatSpinner>(Resource.Id.MediaListFilterDialog_SeasonSpinner), _type, _mediaListFilterModel.Season); SetupYearPicker(_yearPicker = _view.FindViewById <Picker>(Resource.Id.MediaListFilterDialog_YearPicker), _mediaListFilterModel.Year); _selectedGenres = _mediaListFilterModel.IncludedGenres; SetupGenresButton(_genresButton = _view.FindViewById <AppCompatButton>(Resource.Id.MediaListFilterDialog_GenresButton)); _selectedTags = _mediaListFilterModel.IncludedTags; SetupTagsButton(_tagsButton = _view.FindViewById <AppCompatButton>(Resource.Id.MediaListFilterDialog_TagsButton)); _selectedStreamingOn = _mediaListFilterModel.LicensedBy; SetupStreamingOnButton(_streamingOnButton = _view.FindViewById <AppCompatButton>(Resource.Id.MediaListFilterDialog_StreamingOnButton)); SetupTitleEditText( _titleEditText = _view.FindViewById <EditText>(Resource.Id.MediaListFilterDialog_Title)); return(_view); }
public static void UpdateBackButton(this AToolbar nativeToolbar, Toolbar toolbar) { if (toolbar.BackButtonVisible) { var context = nativeToolbar.Context?.GetThemedContext() ?? nativeToolbar.Context ?? toolbar.Handler?.MauiContext?.Context; nativeToolbar.NavigationIcon ??= new DrawerArrowDrawable(context !) { Progress = 1 }; var backButtonTitle = toolbar.BackButtonTitle; ImageSource image = toolbar.TitleIcon; if (!string.IsNullOrEmpty(backButtonTitle)) { nativeToolbar.NavigationContentDescription = backButtonTitle; } else if (image == null || nativeToolbar.SetNavigationContentDescription(image) == null) { nativeToolbar.SetNavigationContentDescription(Resource.String.nav_app_bar_navigate_up_description); } } else { if (!toolbar.DrawerToggleVisible) { nativeToolbar.NavigationIcon = null; } else { nativeToolbar.SetNavigationContentDescription(Resource.String.nav_app_bar_open_drawer_description); } } nativeToolbar.UpdateIconColor(toolbar); nativeToolbar.UpdateBarTextColor(toolbar); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); SetContentView(Resource.Layout.activity_main); AndroidX.AppCompat.Widget.Toolbar toolbar = FindViewById <AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); DrawerLayout drawer = FindViewById <DrawerLayout>(Resource.Id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close); drawer.AddDrawerListener(toggle); drawer.DrawerOpened += (sender, e) => { updateUsername(); }; toggle.SyncState(); NavigationView navigationView = FindViewById <NavigationView>(Resource.Id.nav_view); navigationView.SetNavigationItemSelectedListener(this); BluetoothHelper.getPermission(BaseContext, this); ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage }, 1); ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, 1); TabaccoList.Init(() => { RaterSaved.Init(() => { if (RaterSaved.GetRater().Name == Rater.DefaultName) { ShowRaterChange(); } else { updateUsername(); ShowTabaccoHome(); } finishedInit = true; }); }); }
public override AView OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (_shellContent != null) { _page = ((IShellContentController)_shellContent).GetOrCreateContent(); } _root = inflater.Inflate(Resource.Layout.shellcontent, null).JavaCast <CoordinatorLayout>(); _toolbar = _root.FindViewById <AToolbar>(Resource.Id.shellcontent_toolbar); _renderer = Platform.CreateRenderer(_page, Context); Platform.SetRenderer(_page, _renderer); _shellPageContainer = new ShellPageContainer(Context, _renderer); if (_root is ViewGroup vg) { vg.AddView(_shellPageContainer); } _toolbarTracker = _shellContext.CreateTrackerForToolbar(_toolbar); _toolbarTracker.Page = _page; // this is probably not the most ideal way to do that _toolbarTracker.CanNavigateBack = _shellContent == null; _appearanceTracker = _shellContext.CreateToolbarAppearanceTracker(); ((IShellController)_shellContext.Shell).AddAppearanceObserver(this, _page); if (_shellPageContainer.LayoutParameters is CoordinatorLayout.LayoutParams layoutParams) { layoutParams.Behavior = new AppBarLayout.ScrollingViewBehavior(); } return(_root); }
public override AView OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { var shellSection = ShellSection; if (shellSection == null) { return(null); } if (shellSection.CurrentItem == null) { throw new InvalidOperationException($"Content not found for active {shellSection}. Title: {shellSection.Title}. Route: {shellSection.Route}."); } var root = inflater.Inflate(Resource.Layout.rootlayout, null).JavaCast <CoordinatorLayout>(); _toolbar = root.FindViewById <AToolbar>(Resource.Id.maui_toolbar); _viewPager = root.FindViewById <FormsViewPager>(Resource.Id.main_viewpager); _tablayout = root.FindViewById <TabLayout>(Resource.Id.main_tablayout); _viewPager.EnableGesture = false; _viewPager.AddOnPageChangeListener(this); _viewPager.Id = Platform.GenerateViewId(); _viewPager.Adapter = new ShellFragmentPagerAdapter(shellSection, ChildFragmentManager); _viewPager.OverScrollMode = OverScrollMode.Never; _tablayout.SetupWithViewPager(_viewPager); Page currentPage = null; int currentIndex = -1; var currentItem = ShellSection.CurrentItem; while (currentIndex < 0 && SectionController.GetItems().Count > 0 && ShellSection.CurrentItem != null) { currentItem = ShellSection.CurrentItem; currentPage = ((IShellContentController)shellSection.CurrentItem).GetOrCreateContent(); // current item hasn't changed if (currentItem == shellSection.CurrentItem) { currentIndex = SectionController.GetItems().IndexOf(currentItem); } } _toolbarTracker = _shellContext.CreateTrackerForToolbar(_toolbar); _toolbarTracker.Page = currentPage; _viewPager.CurrentItem = currentIndex; if (SectionController.GetItems().Count == 1) { _tablayout.Visibility = ViewStates.Gone; } _tablayout.LayoutChange += OnTabLayoutChange; _tabLayoutAppearanceTracker = _shellContext.CreateTabLayoutAppearanceTracker(ShellSection); _toolbarAppearanceTracker = _shellContext.CreateToolbarAppearanceTracker(); HookEvents(); return(_rootView = root); }
static void UpdateMenuItem(AToolbar toolbar, ToolbarItem item, int?menuItemIndex, IMauiContext mauiContext, Color?tintColor, PropertyChangedEventHandler toolbarItemChanged, List <IMenuItem> previousMenuItems, List <ToolbarItem> previousToolBarItems, Action <Context, IMenuItem, ToolbarItem>?updateMenuItemIcon = null) { var context = mauiContext.Context ?? throw new ArgumentNullException($"{nameof(mauiContext.Context)}"); 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.ToPlatform() : tintColor.MultiplyAlpha(0.302f).ToPlatform(); 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 || menuItemIndex >= previousMenuItems?.Count) { menuitem = menu.Add(0, AView.GenerateViewId(), 0, newTitle) ?? throw new InvalidOperationException($"Failed to create menuitem: {newTitle}"); previousMenuItems?.Add(menuitem); } else { if (previousMenuItems == null || previousMenuItems.Count < menuItemIndex.Value) { return; } menuitem = previousMenuItems[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(mauiContext, menuitem, item, tintColor); } if (item.Order != ToolbarItemOrder.Secondary) { menuitem.SetShowAsAction(ShowAsAction.Always); } menuitem.SetOnMenuItemClickListener(new GenericMenuClickListener(((IMenuItemController)item).Activate)); if (item.Order != ToolbarItemOrder.Secondary && !PlatformVersion.IsAtLeast(26) && (tintColor != null && tintColor != null)) { var view = toolbar.FindViewById(menuitem.ItemId); if (view is ATextView textView) { if (item.IsEnabled) { textView.SetTextColor(tintColor.ToPlatform()); } else { textView.SetTextColor(tintColor.MultiplyAlpha(0.302f).ToPlatform()); } } } }