How To Start Developing An Android App Using Xamarin In Visual Studio 2015

Xamarin is a free cross platform mobile Application development tool, using C#. We can create native iOS, Android and Windows apps , using Xamarin tools.

Let’s see the steps


Start a new instance of Visual Studio 2015 and go to File-> New -> Project, select blank Android under Visual C# Templates, as shown below.



By default, the Application contains the folder structure, mentioned below.



Assets Folder

It contains the files (text,json.etc.) required by your Application.

Resource Folder

Resource folder contains the Application resources such as strings, images and layouts.

Here, I am going to show you how to add one button and change the button text when the user clicks the button.

Open Main.axml file in your project and add one button. You can directly drag and drop the button from the tools or go to source view and add the button, using the code, mentioned below.



You can directly write the button text or getting from Strings.xml file. Here, I am referring to the button text from Strings.xml file.

Now, go to MainActivity.cs file and write the code, mentioned below to change the button text.

  1. [Activity(Label = "DemoAndroid", MainLauncher = true, Icon = "@drawable/icon")]  
  2. public class MainActivity: Activity  
  3. {  
  4.   
  5.   
  6.     protected override void OnCreate(Bundle bundle) {  
  7.         base.OnCreate(bundle);  
  8.   
  9.         // Set our view from the "main" layout resource  
  10.         SetCotentView(Resource.Layout.Main);  
  11.   
  12.         // Get our button from the layout resource,  
  13.         // and attach an event to it  
  14.         Button button = FindViewById < Button > (Resource.Id.MyButton);  
  15.         button.Click += delegate {  
  16.             button.Text = "Welcome to C# Corner";  
  17.         };  
  18.     }  
  19.   
  20.   
  21. }  
OnCreate method will run automatically when the Application starts because we set mainactivity, which is the main launcher. Set our view by using SetContentView method by passing the required view. Here, I am passing Main.axml view. Now, we need to get the button, using find view by Id and access the button properties. On button click event, I changed the button text to Welcome to C# Corner.

Now, run the Application and see the output, as shown below. We can deploy the Application, using Emulator or directly to the device.

  

Up Next
    Ebook Download
    View all
    Learn
    View all