I'm developing an app in Xamarin Android. I want to use a button and then move to the next layout (I have 3 layouts). I'm able to move from layout1 to layout2 but not from layout 2 to layout 3.
LayoutOne
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#1f1e1e"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:text="One"
android:clickable="true"
android:id="@ id/btnOne"
android:textSize="16sp"
android:layout_width="wrap_content"
android:backgroundTint="#484848"
android:layout_marginTop="3dp"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"/>
</LinearLayout>
//////////////////////////////////
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.LayoutOne);
Button btnOne = FindViewById<Button>(Resource.Id.btnOne);
btnOne.Click = btnOne_Click;
}
private void btnOne_Click(object Sender, EventArgs e)
{
SetContentView(Resource.Layout.LayoutTwo);
}
LayoutTwo
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#1f1e1e"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:text="Two"
android:clickable="true"
android:id="@ id/btnTwo"
android:textSize="16sp"
android:layout_width="wrap_content"
android:backgroundTint="#484848"
android:layout_marginTop="3dp"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"/>
</LinearLayout>
//////////////////////////////////
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.LayoutTwo);
Button btnTwo = FindViewById<Button>(Resource.Id.btnTwo);
btnTwo.Click = btnTwo_Click;
}
private void btnTwo_Click(object Sender, EventArgs e)
{
SetContentView(Resource.Layout.LayoutThree);
}
LayoutThree
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#1f1e1e"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:textSize="16sp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This is Layout3"
android:layout_gravity="center_horizontal"
android:textColor="@android:color/white"/>
</LinearLayout>
There is no errors and the app runs. I run it through my phone using usb debugging.
CodePudding user response:
If you want to navigate between three different layouts,a common method is to create three different activities and set the three layouts for them.
For example, FirstActivity
loads LayoutOne
,SecondActivity
loads LayoutTwo
and ThirdActivity
loads LayoutThree
, after that we can use Intent
to navigate between them.
Please refer to the following code:
FirstActivity.cs
public class FirstActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.LayoutOne);
Button btnOne = FindViewById<Button>(Resource.Id.btnOne);
btnOne.Click = BtnOne_Click;
}
private void BtnOne_Click(object sender, System.EventArgs e)
{
// SetContentView(Resource.Layout.LayoutTwo);
Intent intent = new Intent(this,typeof(SecondActivity));
StartActivity(intent);
}
}
SecondActivity.cs
public class SecondActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.LayoutTwo);
Button btnTwo = FindViewById<Button>(Resource.Id.btnTwo);
btnTwo.Click = btnTwo_Click;
}
private void btnTwo_Click(object sender, EventArgs e)
{
//SetContentView(Resource.Layout.LayoutThree);
Intent intent = new Intent(this,typeof(ThirdActivity));
StartActivity(intent);
}
}
ThirdActivity.cs
public class ThirdActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.LayoutThree);
}
}