Skip to content

Ellery Yang

I write about products and the PM role.

Menu
  • Home
  • About me
Menu

Developing Android app in C# with Visual Studio 2015

Posted on July 27, 2015April 25, 2020 by Ellery

 

Visual Studio 2015 integrates Xamarin platform so users can write Android apps in C#. I took advantage of some free time after work, got Visual Studio 2015 Enterprise, and made a simple Android app. In this article I will talk about the first steps in making Android apps from VS 2015.

After installing VS 2015 with Xamarin components, a Xamarin license is required to make Android apps. You can get a free trial from Xamarin, or, if you are a student, get a DreamSpark subscription to obtain a free license.

A good tutorial to get started on Android in VS 2015 can be found on this MSDN article. The following is how I made my Android app.

The first thing I noticed was the different Java coding styles in native Android and Xamarin Android. Class libraries from different frameworks, different cases, properties in one that are implemented as methods in the other, etc. For example, take a look at the my code to create an image file:

private Java.IO.File createImageFile()
{
    // Create an image file name
    string timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").Format(new Java.Util.Date());
    string imageFileName = "JPEG_" + timeStamp + "_";
    Java.IO.File storageDir = Android.OS.Environment.GetExternalStoragePublicDirectory(
    Android.OS.Environment.DirectoryPictures);
    Java.IO.File image = Java.IO.File.CreateTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
        );

    // Save a file: path for use with ACTION_VIEW intents
    string mCurrentPhotoPath = "file:" + image.AbsolutePath;
    return image;
}

And the code used by native android from developer.android.com:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

Notice the differences between method signatures, method names (GetExternalStoragePublicDirectory vs. getExternalStoragePublicDirectory), and class references (for example, in Xamarin, File has to be replaced by Java.IO.File to avoid confusion with System.IO.File).

Other than this, Xamarin android development is much similar to native. What is truly amazing is the ability to use .NET as the sole logic core for cross-platform apps. That being said, if an application is logic-driven, Xamarin will make cross-platform development a breeze; if, however, an app is presentation-driven, native development is probably much more powerful.

To make this application, I went over the following steps:

1. Subscribe to Microsoft’s Face API.

2. In VS 2015, start a new Android project, add reference to Client library provided by Face API.

3. Design UI in Main.axml and add corresponding code to MainActivity.cs. Code in MainActivity.cs gets a Bitmap from a chooser or the camera, scales it to proper size, uses Face API to detect human faces then mark them with a Paint and a Canvas. Notice all of these are Android functions, but the Face API library that it uses is 100% .NET.

4. For debugging and testing, I use the emulator that came with Xamarin Android, which you can choose to install when installing VS 2015. If you want to use an actual Android phone to debug, here is a tutorial.

You can download the source code (Android part) of my project here and check out a video demo here.

PM Blog (2017 - present)

  • July 2025
  • April 2024
  • November 2023
  • September 2023
  • March 2023
  • January 2023
  • September 2022
  • July 2022
  • February 2022
  • September 2021
  • August 2021
  • April 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • July 2019
  • June 2019
  • April 2019
  • March 2019
  • December 2018
  • October 2018
  • May 2018
  • March 2018
  • December 2017

A Student's Blog (2015 - 2017)

  • July 2017
  • June 2017
  • August 2016
  • July 2016
  • April 2016
  • March 2016
  • February 2016
  • December 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
© 2025 Ellery Yang | Powered by Minimalist Blog WordPress Theme