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

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 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>  
  6. The code behind for this is in MainActivity.cs file. Replace the method in the OnCreate method with the following code.
    base.OnCreate(bundle);
    1. // Set our view from the "main" layout resource  
    2. SetContentView(Resource.Layout.Main);  
    3. // Get our button from the layout resource,  
    4. // and attach an event to it  
    5. Buttonbutton = FindViewById < Button > (Resource.Id.MyButton);  
    6. TextViewconnectionStatus = FindViewById < TextView > (Resource.Id.connectionStatus);  
    7. TextViewconnectionType = FindViewById < TextView > (Resource.Id.connectionType);  
    8.   
    9. button.Click += delegate   
    10. /*button.Text = string.Format("{0} clicks!", count++);*/  
    11.   
    12.     ConnectivityManagerconnectivityManager = (ConnectivityManager) GetSystemService(ConnectivityService);  
    13.   
    14.     NetworkInfoactiveConnection = connectivityManager.ActiveNetworkInfo;  
    15.     boolisOnline = (activeConnection != null) && activeConnection.IsConnected;  
    16.   
    17.     if (isOnline)   
    18.     {  
    19.         connectionStatus.Text = "You are ONLINE";  
    20.         //check if connected on wifi  
    21.         NetworkInfowifiInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);  
    22.         if (wifiInfo.IsConnected)   
    23.         {  
    24.             connectionType.Text = string.Format("{0} - {1}", activeConnection.Type, activeConnection.TypeName);  
    25.         } else {  
    26.             //check for roaming  
    27.             connectionType.Text = string.Format("{0} - {1}, Roaming:- {3}", activeConnection.Type, activeConnection.TypeName, activeConnection.IsRoaming);  
    28.         }  
    29.     } else   
    30.     {  
    31.         connectionStatus.Text = "You are OFFLINE, please check your connection";  
    32.     }  
    33. };  
  7. 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 to download 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 to enter the xamarin credentials when you use the xamarin emulator for debugging your application.

    xamrin

    This is the xamarian emulator.

    emulator

    For 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 chose the appropriate device for testing. The VS emulator can be launched from the Start menu.

    Alternately 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

Up Next
    Ebook Download
    View all
    Learn
    View all