2

I am new to xamarin android I have given up on forms and I am wanting to change layout onnavigationselecteditem and was wondering if someone can point me in right direction I tried using setContentView but that did not change the view at all.

There has been a layout created with the name of jobs so i no its not null I wish people would stop flagging questions with a general null statment its not the case here.

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

        if (id == Resource.Id.nav_camera)
        {
            // Handle the camera action
        }
        else if (id == Resource.Id.jobs)
        {
            SetContentView(Resource.Layout.jobs);
        }
        else if (id == Resource.Id.nav_assignjob)
        {

        }
        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);
        return true;
    }

It timed out with the following

Unhandled Exception:

System.NullReferenceException:

On the drawer.CloseDrawer(GravityCompat.Start); line I presume because SetContview left something open but didn't display it.

David B
  • 305
  • 2
  • 13
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Stefan Jun 09 '18 at 18:39
  • it has got nothing to do with that is to genleriised this is for xamrin – David B Jun 09 '18 at 19:11
  • For the record; if you disagree, and are worrying about people flagging it as a basic nullreference question: please provide some proof. It helps ;-) – Stefan Jun 09 '18 at 19:56
  • @DavidB Actvities are not really designed to have their content changed after the `OnCreate` phase, you should either start a new activity or instance/inflate a Fragment that defines your "job view" and replace some fragment holder within the current Activity's layout (this is the approach I would assume you should take based upon your limited code sample and question). – SushiHangover Jun 09 '18 at 21:06

1 Answers1

1

You should use a Fragment if you want to replace a specific layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_frame">
  <LinearLayout
      android:id="@+id/change_that"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
  </LinearLayout>
</FrameLayout>

Using the Replace function of the FragmentTransition:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var view = inflater.Inflate(Resource.Layout.yourLayout, container, false);
    var baseLayout  = view.FindViewById<LinearLayout>(Resource.Id.change_that);
    this.ChangeLayout += (object sender, EventArgs e) =>
    {
        YourFragment newLayout = new YourFragment();
        FragmentTransaction ft = FragmentManager.BeginTransaction();
        ft.Replace(Resource.Id.content_frame, newLayout);
        ft.Commit();
        baseLayout.Visibility = ViewStates.Gone;
    };

    return view;
}

Then you can fire events to change the layout whatever is your logic on the OnNavigationItemSelected.

Hichame Yessou
  • 2,443
  • 2
  • 15
  • 28