Setup Visual Studio 2015 For Android Apps Development And Create First Android App

Introduction

 
Use this link to download Visual Studio 2015 installer. It’s available in the following three editions: Community, Professional and Enterprise. The first one is free, whereas other two are paid versions. For checking the license cost details click here.
 
For this demo, I have used the community (free) edition of Visual Studio 2015.
 
Apart from Visual Studio 2015, we also need the Xamarin visual studio plugin. This is also a free installable. To download click here. The original Xamarin studio is not free. Important thing to note here is that, even when I had the Xamarin studio installed on my machine, I had to run the installer again, as it updates the Android SDK, the new emulators and of course the new visual studio part of it.
 
Creating a new Android Project:
  1. Click File-New Project on the Visual Studio 2015 menu.
     
  2. Once the Xamarin plugin is installed you will notice new templates appearing under the Android sections.
     
    plugin
     
  3. Select the Blank App (Android) template for now.
     
  4. Once the project is created you can check that the default screen (Main.axml) is created under the folder ResourcesLayout
     
    Note- axml stands for Android XML. This is used to define the Android app design layouts.
     
  5. For this demo, let’s add two TextView. Replace the code in the source of the Main.axml with the following code,
    1.     <?xmlversion="1.0"encoding="utf-8"?>    
    2.     <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"    
    3.         android:orientation="vertical"    
    4.         android:layout_width="fill_parent"    
    5.         android:layout_height="fill_parent">    
    6.         <Button    
    7.         android:id="@+id/MyButton"    
    8.         android:layout_width="fill_parent"    
    9.         android:layout_height="wrap_content"    
    10.         android:text="@string/Hello" />    
    11.         <TextView    
    12.             android:text=""    
    13.             android:layout_width="match_parent"    
    14.             android:layout_height="wrap_content"    
    15.             android:id="@+id/connectionStatus" />    
    16.         <TextView    
    17.         android:text=""    
    18.         android:layout_width="match_parent"    
    19.         android:layout_height="wrap_content"    
    20.         android:id="@+id/connectionType" />    
    21.     </LinearLayout>    
    22.   
    23. The code behind for this is in MainActivity.cs file. Replace the method in the OnCreate method with the following code.  
    24.    
    25. base.OnCreate(bundle);  
    26.    
    27.   
    28.     // Set our view from the "main" layout resource    
    29.     SetContentView(Resource.Layout.Main);    
    30.     // Get our button from the layout resource,    
    31.     // and attach an event to it    
    32.     Buttonbutton = FindViewById < Button > (Resource.Id.MyButton);    
    33.     TextViewconnectionStatus = FindViewById < TextView > (Resource.Id.connectionStatus);    
    34.     TextViewconnectionType = FindViewById < TextView > (Resource.Id.connectionType);    
    35.         
    36.     button.Click += delegate     
    37.     { /*button.Text = string.Format("{0} clicks!", count++);*/    
    38.         
    39.         ConnectivityManagerconnectivityManager = (ConnectivityManager) GetSystemService(ConnectivityService);    
    40.         
    41.         NetworkInfoactiveConnection = connectivityManager.ActiveNetworkInfo;    
    42.         boolisOnline = (activeConnection != null) && activeConnection.IsConnected;    
    43.         
    44.         if (isOnline)     
    45.         {    
    46.             connectionStatus.Text = "You are ONLINE";    
    47.             //check if connected on wifi    
    48.             NetworkInfowifiInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);    
    49.             if (wifiInfo.IsConnected)     
    50.             {    
    51.                 connectionType.Text = string.Format("{0} - {1}", activeConnection.Type, activeConnection.TypeName);    
    52.             } else {    
    53.                 //check for roaming    
    54.                 connectionType.Text = string.Format("{0} - {1}, Roaming:- {3}", activeConnection.Type, activeConnection.TypeName, activeConnection.IsRoaming);    
    55.             }    
    56.         } else     
    57.         {    
    58.             connectionStatus.Text = "You are OFFLINE, please check your connection";    
    59.         }    
    60.     };   
  6. Hit F5 to test the code.
     
    app
     
    Once you start the testing of your code you may need to understand the Emulators available as well. With default installation you will get the xamarin emulator which was a little troublesome initially. I would suggest downloading the Visual Studio Emulator.
     
    I encountered some issues with the xamarin_android_api_23 emulator. It was not starting up and hence the app deployment was failing. Hence have downloaded the visual studio emulator from here.
     
    Xamarin login is required. The system may prompt you to enter the xamarin credentials when you use the xamarin emulator for debugging your application.
     
    xamrin
     
    This is the xamarian emulator.
     
    emulator
     
    For the VS emulator please ensure that the machine has 2GB of unused RAM. You can monitor that from the task manager.
     
    The VS Emulator for Android has several device profile options, you can choose the appropriate device for testing. The VS emulator can be launched from the Start menu.
     
    Alternatively, you can launch it directly from “C:\Program Files (x86)\Microsoft Emulator Manager\1.0\emulatormgr.exe”.
     
    profile
     
    Note- If you encounter issues in launching the devices, try running XdeCleanup.exe - In my case: "C:\Program Files (x86)\Microsoft XDE\10.0.10240.0".
     
    The following is the screenshot of a 5” KitKat(4.4) XXHDPI Phone.
     
    phone