Search code examples
androidiosxamarin.iosxamarin.androidbottomnavigationview

Is there an equivalent for shouldSelectViewController in android for BottomNavigationView?


I have a BottomNavigationView in my android activity, which consists of 4 menuItem. When I tap on the downloads menu I check if there are any downloaded contents available, if available I will allow the navigation to happen and if there is no downloaded content I will show a Toast stating the same and I want the previous tab to remain selected. In iOS I can use the delegate method shouldSelectViewController to determine whether navigation can be allowed or not. The method signature is specified below:

- (BOOL)tabBarController:(UITabBarController *)tabBarController 
shouldSelectViewController:(UIViewController *)viewController;

I tried reselecting the previously selected tab, as a result, the previous item is retained but the selected item color is still assigned to the downloads tab.

    private void BottomNavigationItemSelected(object obj, BottomNavigationView.NavigationItemSelectedEventArgs args)
    {
        Android.Support.V4.App.Fragment fragment = null;
        Android.Support.V4.App.Fragment currentFragment = SupportFragmentManager.FindFragmentById(Resource.Id.content_frame);
        string title = "";
        if (args.Item.ItemId == Resource.Id.menu_explore)
        {
            _selectedToolbarId = args.Item.ItemId;
            title = Resources.GetString(Resource.String.shelf_title);
            fragment = _exploreFragment;
            _fragmentTag = "Home";
        }
        else
        {
            title = args.Item.TitleFormatted.ToString();
        }
        if (args.Item.ItemId == Resource.Id.menu_dashboard)
        {
            //COULD BE MADE CONFIGURABLE
            //fragment = _dashboardFragment;
            _selectedToolbarId = args.Item.ItemId;
            fragment = _redesignDashboard;
            _fragmentTag = "Dashboard";
        }
        else if (args.Item.ItemId == Resource.Id.menu_more)
        {
            _selectedToolbarId = args.Item.ItemId;
            fragment = _moreFragment;
            _fragmentTag = "More";
        }
        else if (args.Item.ItemId == Resource.Id.menu_report)
        {
            _selectedToolbarId = args.Item.ItemId;
            fragment = _reportFragment;
            _fragmentTag = "Report";
        }
        else if (args.Item.ItemId == Resource.Id.menu_downloads)
        {
            List<Product> _downloadProducts = DBService.GetDB().GetDownloadedProducts();
            if (_downloadProducts == null || _downloadProducts.Count == 0)
            {
                _bottomNavigationView.SelectedItemId = _selectedToolbarId;
                Toast.MakeText(this, "No downloaded products", ToastLength.Short).Show();
                args.Item.SetChecked(false);
            }
            else
            {
                _downloadGalleryFragment = new DownloadGalleryFragment(_downloadProducts);
                fragment = _downloadGalleryFragment;
                _fragmentTag = "Downloads";
            }
        }
        if (fragment != null)
        {
            _toolbarTitle.Text = title;
            ToggleTitle(true);
            SupportFragmentManager.BeginTransaction().SetCustomAnimations(Resource.Animation.fab_slide_in_from_right, Resource.Animation.fab_slide_out_to_left).Replace(Resource.Id.content_frame, fragment, _fragmentTag).Commit();
        }
    }

<?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/menu_explore"
      android:enabled="true"
      android:title="@string/explore"
      android:icon="@drawable/explore_icon"
      app:showAsAction="always" />

    <item
      android:id="@+id/menu_dashboard"
      android:enabled="true"
      android:title="@string/dashboard"
      android:icon="@drawable/Dashboard_new_icon"
      app:showAsAction="always" />

     <item
      android:id="@+id/menu_report"
      android:enabled="true"
      android:title="@string/reports"
      android:icon="@drawable/dashboard_icon"
      app:showAsAction="always" />

     <item
      android:id="@+id/menu_downloads"
      android:enabled="true"
      android:title="@string/menu_downloads"
      android:icon="@drawable/download_icon"
      app:showAsAction="always" />

     <item
      android:id="@+id/menu_more"
      android:enabled="true"
      android:title="@string/more_bottombar"
      android:icon="@drawable/more_icon"
      app:showAsAction="always" />
</menu> 

Solution

  • Seems like this is an issue with Android or the Bottom Navigation View. When I executed the reselection of the previous fragment after a small delay of 50milliseconds its working fine. ie the reselected fragment or the previous fragments icon gets highlighted as required.

    if (args.Item.ItemId == Resource.Id.menu_downloads)
                {
                    List<Product> _downloadProducts = DBService.GetDB().GetDownloadedProducts();
                    if (_downloadProducts == null || _downloadProducts.Count == 0)
                    {
    
                       _readProgressTimerTask = new Timer
                        {
                            Enabled = true,
                            Interval = 50,
                            AutoReset = false
                        };
                        _readProgressTimerTask.Elapsed += OnProgressCheckTimeElapsed;
                        Toast.MakeText(this, this.Resources.GetString(Resource.String.no_downloads), ToastLength.Short).Show();
                    }
                    else
                    {
                        _downloadGalleryFragment = new DownloadGalleryFragment(_downloadProducts);
                        fragment = _downloadGalleryFragment;
                        _fragmentTag = "Downloads";
                    }
                }
    
    private void OnProgressCheckTimeElapsed(System.Object source, ElapsedEventArgs args)
            {
                this.RunOnUiThread(() =>
                {
                    _bottomNavigationView.SelectedItemId = _selectedToolbarId;
                });
            }