Navigating To Another View And Opening Another Activity In Android Using Xamarin

If your application is a not single page application, you need to navigate to another page to do some other operations and also open another activity in an Android application.

Let’s see the steps given below.

Create new Android project, as shown below.



Now, go to your Solution Explorer. Right click on the layout folder and add new layout page, as shown below.



Now, add new activity file by right clicking on your project and adding a new activity, as shown below.



Subsequently, go to your Main.axml page and write the code given below. One button is for going to page1 and another is for opening activity1.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     >  
  7. <Button    
  8.     android:id="@+id/Button1"  
  9.     android:layout_width="match_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="Go to page1"  
  12.     />  
  13. <Button    
  14.     android:id="@+id/Button2"  
  15.     android:layout_width="match_parent"   
  16.     android:layout_height="wrap_content"   
  17.     android:text="Open Activity1"  
  18.     />  
  19. </LinearLayout>  
Now, go to MainActivity.cs file and write the code given below to navigate and open the new activity.

First, get the properties for Button1 and Button2 and subscribe to the button click event.
  1. protected override void OnCreate(Bundle bundle)  
  2.         {  
  3.             base.OnCreate(bundle);  
  4.   
  5.             // Set our view from the "main" layout resource  
  6.             SetContentView(Resource.Layout.Main);  
  7.              
  8.             Button button1 = FindViewById<Button>(Resource.Id.Button1);  
  9.             button1.Click += Button_Click;  
  10.             Button button2 = FindViewById<Button>(Resource.Id.Button2);  
  11.             button2.Click += Button2_Click;           
  12.         }  
Now, write the code given below in button1 click event to navigate to page1.
  1. private void Button_Click(object sender, EventArgs e)  
  2.         {  
  3.   
  4.             SetContentView(Resource.Layout.Page1);  
  5.         }  
Now, go to button2 click event and write the code given below to open the activity1. 
  1. private void Button2_Click(object sender, EventArgs e)  
  2.         {  
  3.             Intent intent = new Intent(thistypeof(Activity1));  
  4.             StartActivity(intent);  
  5.         }  
In Page 1, add TextView control to display that we are in the Page1.axml, as shown below.



Now, add the code given below that shows we are in the activity1. 
  1. public class Activity1: Activity  
  2. {  
  3.     protected override void OnCreate(Bundle savedInstanceState) {  
  4.         base.OnCreate(savedInstanceState);  
  5.   
  6.         Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(this);  
  7.         AlertDialog alert = dialog.Create();  
  8.         alert.SetTitle("Alert");  
  9.         alert.SetMessage("Activity1 opened");  
  10.         alert.SetButton("OK", (c, ev) => {  
  11.   
  12.         });  
  13.         alert.Show();  
  14.     }  
  15. }  
Now, run the app and check the output, as shown below.

 






Similar Articles