Reputation: 81
I am new to Xamarin and building a native app. I saw many sites explaining the left side navigation menu but they do not explain how to open other pages.
I wanted to show different content pages on the navigation click.
Please tell me how can I do this.
Thank you in advance!!!
Upvotes: 1
Views: 167
Reputation: 10948
In Xamarin.Android, when you want to navigate to a new layout, you could follow the steps below.
Create the layout in Resource/Layout folder
and create acticity.cs
in app project
TextLayout.xml
<TextView
android:id="@+id/tv_Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
GralleryLayout.xml
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Add the control in main layout.
<Button
android:id="@+id/btn_TextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Layout"/>
<Button
android:id="@+id/btn_GralleryLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_TextLayout"
android:text="Grallery Layout"/>
Add the code below to navigate to the activity you want.
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.activity_main);
var btn_TextLayout = FindViewById<Button>(Resource.Id.btn_TextLayout);
btn_TextLayout.Click += Btn_TextLayout_Click;
var btn_GralleryLayout = FindViewById<Button>(Resource.Id.btn_GralleryLayout);
btn_GralleryLayout.Click += Btn_GralleryLayout_Click;
}
private void Btn_GralleryLayout_Click(object sender, System.EventArgs e)
{
var intent = new Intent(this, typeof(GralleryActivity));
StartActivity(intent);
}
private void Btn_TextLayout_Click(object sender, System.EventArgs e)
{
var intent = new Intent(this, typeof(TextActivity));
StartActivity(intent);
}
Open the view of TextLayout and GralleryLayout.
TextActivity.cs
SetContentView(Resource.Layout.TextLayout);
var tv_Text = FindViewById<TextView>(Resource.Id.tv_Text);
tv_Text.Text = "Only the text";
GralleryActvity.cs
var ImageView = FindViewById<ImageView>(Resource.Id.imageView1);
ImageView.SetImageResource(Resource.Drawable.pig);
Result:
You could download from the Android/App1 folder
on GitHub for reference.
https://github.com/WendyZang/Test.git
Upvotes: 1