Stefan van de Laarschot
Stefan van de Laarschot

Reputation: 2163

Xamarin Android application

Started today to develop with xamarin in Visual studio it looks nice so far But my question is

how to switch from button click to another Layout(View)

When i do this on my login page looks as follow:

[Activity(Label = "Test", MainLauncher = true, Icon = "@drawable/icon")]
public class Test: Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Afvalwijzer);

        EditText Bla1 = FindViewById<EditText>(Resource.Id.Bla1);
        Bla1.Hint = "Your text";

        EditText Bla2= FindViewById<EditText>(Resource.Id.Bla2);
        Bla2.Hint = "Your text";

        Button Login = FindViewById<Button>(Resource.Id.BtnLogin);
        Login.Click += new EventHandler(Login_Click);

    }

    void Login_Click(object sender, EventArgs e)
    {
        EditText Bla1 = FindViewById<EditText>(Resource.Id.Bla1);
        EditText Bla2 = FindViewById<EditText>(Resource.Id.Bla2 );

        if (!String.IsNullOrEmpty(Bla1.Text) && !String.IsNullOrEmpty(Bla2.Text))
        {
            AfvalwijzerWS WebS = new AfvalwijzerWS();
            var Data = WebS.Authenticate(Bla1.Text, Bla2.Text);
            TextView txt = FindViewById<TextView>(Resource.Id.ContentTextView);

            if (Data.Count() > 0)
            {
                SetContentView(Resource.Layout.Index);
            }
            else
            {
                MessageBox("No Access !");
            }
        }
        else
        {
            MessageBox();
        }
    }

This Works fine. But when im on Index(Layout) i have the same code like this:

 protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Index);
        Button Contact = FindViewById<Button>(Resource.Id.BtnContact);
        Contact.Click += (sender, e) =>
        {
            SetContentView(Resource.Layout.Contact);
        };

    }

and its referenced in the xml of the layout ofc but it wont trigger the button event does someone know why ?

Upvotes: 1

Views: 6033

Answers (1)

DaveDev
DaveDev

Reputation: 42175

You should be starting a new activity, rather than setting the content view

 protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Index);
        Button Contact = FindViewById<Button>(Resource.Id.BtnContact);
        Contact.Click += (sender, e) =>
        {
            StartActivity(new Intent(this, typeof(/* whatever activity you want */ )));

            // e.g.
            //StartActivity(new Intent(this, typeof(SplashActivity)));
        };

    }

Upvotes: 5

Related Questions