how to navigate through multiple layouts, from one axml layout to another by the click of a button

I am working on a project with multiple axml layouts, I want to move from one layout to the other by the click of a button. When I move from my main page to login page by using the setcontentview function, it works but when I use similar codes to move to other layouts it doesn't work.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    After using SetContentView to navigate to a new layout, you have to register the new layout's control event on the activity. Otherwise, you can't access the button's click event on the new layout.
    Usually, when we want to display a new page we use Intent to show a new activity. In that new activity, we could set the content view, register the button's event on the OnCreate lifecycle event:

    // Show a new activity
    Intent intent = new Intent(this, typeof(SecondActivity));
    StartActivity(intent);
    
    // The new activity's OnCreate event
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    
        // Create your application here
    
        SetContentView(Resource.Layout.second_layout);
        Button btn = FindViewById<Button>(Resource.Id.button);
        btn.Click += Btn_Click;
    }
    
Sign In or Register to comment.