Add dynamic action menu toolbar on fragments

YeremiaPriambodoYeremiaPriambodo ✭✭USMember ✭✭
edited June 2018 in Xamarin.Android

I want to add action menu like save,edit,etc on toolbar. I'm using App Drawer template from @JamesMontemagno.
How to add the action menu at toolbar dynamically. So it can change every time I choose different fragment. What should I put on main activity and fragment while I use OnOptionsItemSelected for app drawer menu?

this is mainactivity

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.main);
            var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            if (toolbar != null)
            {
                SetSupportActionBar(toolbar);
                SupportActionBar.SetDisplayHomeAsUpEnabled(true);
                SupportActionBar.SetHomeButtonEnabled(true);
            }

            drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);

            //Set hamburger items menu
            SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);

            //setup navigation view
            navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);

            //handle navigation
            navigationView.NavigationItemSelected += (sender, e) =>
            {
                if (previousItem != null)
                    previousItem.SetChecked(false);

                navigationView.SetCheckedItem(e.MenuItem.ItemId);

                previousItem = e.MenuItem;

                switch (e.MenuItem.ItemId)
                {
                    case Resource.Id.nav_home_1:
                        ListItemClicked(0);
                        break;
                    case Resource.Id.nav_home_2:
                        ListItemClicked(1);
                        break;
                }


                drawerLayout.CloseDrawers();
            };


            //if first time you will want to go ahead and click first item.
            if (savedInstanceState == null)
            {
                navigationView.SetCheckedItem(Resource.Id.nav_home_1);
                ListItemClicked(0);
            }
        }

        int oldPosition = -1;
        private void ListItemClicked(int position)
        {
            //this way we don't load twice, but you might want to modify this a bit.
            if (position == oldPosition)
                return;

            oldPosition = position;

            Android.Support.V4.App.Fragment fragment = null;
            switch (position)
            {
                case 0:
                    fragment = Fragment1.NewInstance();
                    break;
                case 1:
                    fragment = Fragment2.NewInstance();
                    break;
            }

            SupportFragmentManager.BeginTransaction()
                .Replace(Resource.Id.content_frame, fragment)
                .Commit();
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.nav_menu, menu);

            return base.OnCreateOptionsMenu(menu);
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Android.Resource.Id.Home:
                    drawerLayout.OpenDrawer(GravityCompat.Start);
                    return true;
            }
            return base.OnOptionsItemSelected(item);
        }

and this is the fragment

            public override void OnCreate(Bundle savedInstanceState)
                    {
                        base.OnCreate(savedInstanceState);

                        // Create your fragment here
                    }

                    public static Fragment2 NewInstance()
                    {
                        var frag2 = new Fragment2 { Arguments = new Bundle() };
                        return frag2;
                    }



                    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
                    {
                        HasOptionsMenu=true;
                        var ignored = base.OnCreateView(inflater, container, savedInstanceState);
                        return inflater.Inflate(Resource.Layout.fragment2, null);
                    }

                    public override bool OnCreateOptionsMenu(IMenu menu)
                    {
                        MenuInflater.Inflate(Resource.Menu.nav_menu, menu);
                        return base.OnCreateOptionsMenu(menu);
                    }


                    public override bool OnOptionsItemSelected(IMenuItem item)
                    {
                        switch (item.ItemId)
                        {
                            case Resource.Id.action_edit:
                                Toast.MakeText(this, "You pressed edit action!", ToastLength.Short).Show();
                                break;
                            case Resource.Id.action_save:
                                Toast.MakeText(this, "You pressed save action!", ToastLength.Short).Show();
                                break;
                        }
                        return base.OnOptionsItemSelected(item);
                    }

thanks in advance

Answers

Sign In or Register to comment.