Cannot call Other Layout from Another Layout by Button Click

muhuri03muhuri03 Member ✭✭
edited December 2018 in Xamarin.Forms

Sir,
I am very new in xamarin. I have three layout. 1) main.axml, 2) layLogIn.axml, 3) layRegistration.axml and have three Activity file 1) mailActivity.cs 2) layLogInActivity.cs and 3) ActivityRegistration.cs

When App start main.axml Layout start first, then from main layout I call layLogIn layout by button click. Then, when I try to call layRegistration layout from layLogIn by Button click, it not working.

Codes:

mailActivity.cs

namespace BCCSJr
{
    [Activity(Label = "BCCS Junior", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        //public static void SetHasBackButton();
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            Button btnLogIn = FindViewById<Button>(Resource.Id.btnLogIn);
            btnLogIn.Click += btnLogIn_Click;

            Button btnRegistration = FindViewById<Button>(Resource.Id.btnRegistration);
            btnRegistration.Click += btnRegistration_Click;

        }

        private void btnLogIn_Click(object sender, EventArgs e)
        {

            SetContentView(Resource.Layout.layLogIn);

        }

        private void btnRegistration_Click(object sender, EventArgs e)
        {

            SetContentView(Resource.Layout.layRegistration);
        }

    }
}

Codes for layLogInActivity.cs

namespace BCCSJr
{
    [Activity(Label = "layLogIn", MainLauncher = false, Icon = "@drawable/icon")]
    public class layLogInActivity : Activity
    {
        protected override void OnCreate(Bundle OnSaveInstanceState)
        {
            base.OnCreate(OnSaveInstanceState);
            SetContentView(Resource.Layout.layLogIn);

            Button btnLogInActivity = FindViewById<Button>(Resource.Id.btnLogInActivity);
            //btnLogInActivity.Click += btnLogInActivity_Click;

            //try to call layRegistration.axml layout
        btnLogInActivity.Click += delegate
                {
                    //txtLogError.Text = "You are in Log In page";
                    SetContentView(Resource.Layout.layRegistration);

                };


            // Create your application here
        }

        private void btnLogInActivity_Click(object sender, EventArgs e)
            {
        SetContentView(Resource.Layout.layRegistration);
        }


    }
}

Answers

  • muhuri03muhuri03 Member ✭✭

    Please do help...

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I have three layout. 1) main.axml, 2) layLogIn.axml, 3) layRegistration.axml

    Those are android layouts, not Xamarin.Forms pages. And your code is android-specific, not platform-agnostic code that we would expect with a Xamarin.Forms app.
    You posted in Xamarin.Forms section.
    You probably meant to post in Xamarin.Android section if you are making an Android-specific program and not a cross-platform application.

    Xamarin.Forms is the cross-platform ecosystem where your views would be XAML not AXML - and your views would be the same shared files for all platforms. Where AXML would be just Android.

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai
    edited December 2018

    @muhuri03 ,

    Cannot call Other Layout from Another Layout by Button Click

    If you want to open another Activity when user click button, you could use StartActivity() instead, here is a simple demo: start_an_activity

    Then, when I try to call layRegistration layout from layLogIn by Button click, it not working.

    Please read the Android document:

    Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.

    For your case, you should execute SetContentView() only once in OnCreate(). Then, when user click the button, open the related Activity.

  • muhuri03muhuri03 Member ✭✭

    @ YorjGo,
    Sir, When I call layLogIn.axml from mail.axml, it working fine, but after call layLogIn.axml, when I call layRegistration.axml from layLogIn.axml by Button Click, it not working. Please Help

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai

    @muhuri03, set a unique layout for each Activity :

    public class MainActivity : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                SetContentView (Resource.Layout.Main);
           }
       }
    
    public class layLogInActivity : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                SetContentView (Resource.Layout.layLogIn);
           }
       }
    
    public class layRegistrationActivity : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                SetContentView (Resource.Layout.layRegistration);
           }
       }
    

    When click the button open different Activity instead of use the SetContentView() method:

    Intent intent = new Intent(this, typeof(TheActivityYouNeedToOpen));
    StartActivity(intent);
    
Sign In or Register to comment.