Home » c# » c# – Xamarin Android Drawable Navbar Item Clickable-Exceptionshub

c# – Xamarin Android Drawable Navbar Item Clickable-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I’m new to Xamarin, I’m trying to figure out that when I click on one of the item on the navbar it would proceed to another activity or another layout but I’ve tried to used the switch but it doesn’t make changes. Here is my code

MainActivity.cs

 public class MainDashboard : AppCompatActivity
    {
        Android.Support.V7.Widget.Toolbar mainToolbar;
        Android.Support.V4.Widget.DrawerLayout drawerLayout;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(PediburRatingApp.Resource.Layout.MainDashboard);
            // Create your application here
            ConnectControl();
        }
        void ConnectControl()
        {
            drawerLayout = (Android.Support.V4.Widget.DrawerLayout)FindViewById(Resource.Id.drawerLayout);
            mainToolbar = (Android.Support.V7.Widget.Toolbar)FindViewById(Resource.Id.mainToolbar);
            SetSupportActionBar(mainToolbar);
            SupportActionBar.Title = "";
            Android.Support.V7.App.ActionBar actionBar = SupportActionBar;
            actionBar.SetHomeAsUpIndicator(Resource.Mipmap.ic_menu_action);
            actionBar.SetDisplayHomeAsUpEnabled(true);
        }
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {

                case Android.Resource.Id.Home:
                    drawerLayout.OpenDrawer((int)GravityFlags.Left);
                    return true;

                case Resource.Id.navProfile:

                    return true;
                default:
                    return base.OnOptionsItemSelected(item);
            }
            return base.OnOptionsItemSelected(item);
        }
    }

Main Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/drawerLayout">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            layout="@layout/include_main"
 />

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navView"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:layout_gravity="start"
        app:headerLayout="@layout/headerLayout"
        app:menu="@menu/navmenu"
    />
</android.support.v4.widget.DrawerLayout>

If I’ve done wrong please correct me

Here is the Navmenu.xml

<?xml version="1.0" encoding="utf-8" ?>
<!--For all properties see: https://aka.ms/android-menu-resource-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">

  <group>
    <item
      android:id="@+id/navProfile"
      android:title="Profile"
      android:icon="@mipmap/ic_person_action"
      />
    <item
  android:id="@+id/navRating"
  android:title="Rate a Driver"
  android:icon="@mipmap/ic_person_action"
      />
    <item
  android:id="@+id/navTopDriver"
  android:title="Weekly Driver"
  android:icon="@mipmap/ic_person_action"
      />
    <item
  android:id="@+id/navBalance"
  android:title="View Balance"
  android:icon="@mipmap/ic_person_action"
      />
    <item
  android:id="@+id/navAbout"
  android:title="About"
  android:icon="@mipmap/ic_person_action"
      />

  </group>

</menu>

How to&Answers:

Do you want to achieve the result like following GIF?

enter image description here

If so, you can achieve it by Fragment

Here is code. when switch Fragments.

      public bool OnNavigationItemSelected(IMenuItem item)
    {
        int id = item.ItemId;

        FragmentTransaction transaction = this.FragmentManager.BeginTransaction();


        if (id == Resource.Id.nav_camera)
        {
            // Handle the camera action

        }
        else if (id == Resource.Id.nav_gallery)
        {
            fragment = new Fragment1();

        }
        else if (id == Resource.Id.nav_slideshow)
        {
            fragment = new Fragment2();

        }
        else if (id == Resource.Id.nav_manage)
        {

        }
        else if (id == Resource.Id.nav_share)
        {

        }
        else if (id == Resource.Id.nav_send)
        {

        }

        DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        drawer.CloseDrawer(GravityCompat.Start);
        transaction.Replace(Resource.Id.FramePage, fragment);
        transaction.Commit();
        return true;
    }

Here is my demo(For testing, I just create two Fragments(Gallery and slidehow tab)), you can refer to it.

https://github.com/851265601/XAndroidFragmentAndNavigationVIew