More than one * Layout * in the app
How do I instantiate a new screen, I have the Main.axml and wanted when I clicked the button to call my Result.axml . I would like to know how I do this in Xamarin in C#. What if I need 2 activity's one for each screen.
I also need to pass values from my first activity to my secondary (layout) activity.. how do I do this with c # (xamarin studio)?
2 answers
You must create a Intent
and call StartActivity
with this Intent
. To pass the parameters, use the extras
that act as the key-value to store the parameters.
Notice that each axml is with a Activity
in this example and it would be the simplest way to achieve what you need. I would have how to do everything answer in the same Activity
but I think the solution would be more complex to understand.
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace PrimeiraAtividade
{
[Activity (Label = "PrimeiraAtividade", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.LayoutUm);
bttn.Click += delegate {
var activity2 = new Intent (this, typeof(Result));
activity2.PutExtra ("parametro1", edt.Text);
StartActivity (ractivity2ess);
};
}
}
}
Second Activity:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace SegundaAtividade
{
[Activity (Label = "SegundaAtividade")]
public class Result : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create your application here
SetContentView (Resource.Layout.LayoutDois);
TextView sm = FindViewById<TextView> (Resource.Id.meu_texto);
sm.Text = Intent.GetStringExtra ("parametro2") ?? "Erro";
}
}
}
Try to do this
var intent = new Intent(this, typeof(layoutDois));
intent.PutStringArrayListExtra("Lista", Lista);
StartActivity(intent);
For more doubts the shamarim website has the first steps
https://developer.xamarin.com/guides