Android First App using Visual Studio


This post describes how to create your first Android app. Have a look at the previous post that shows you how to get set up. You will need a computer with Visual Studio installed and an Android device such as a phone or tablet. You will need to install Xamarin when you install Visual Studio (or you can install it later if you want). You will also need to put your Android into Developer mode.

This post is based on the video called Getting Started with Xamarin Android Modifying the UI on YouTube. It goes just one step past the typical “Hello World” app.

On many Android devices you can take a screenshot of you device by pressing the on button and the home button at the same time, although it may take a few attempts to get it to work. Alternatively you could set up “palm swipe to capture”. Go to Settings, Controls. Look for Palm motion or Motions and Gestures. Turn on Capture screen. Screenshot help with development communications and documentation.

How To

Here are the steps to create an Android app called App2 based on the video.

  1. Start Visual Studio with Xamarin and click File, New, Project and select Android (under Templates, Visual C#, Windows) and select Blank App (Android) and select a location on your hard drive at the bottom and change the name to App2 (if not App2) and leave Create directory for solution checked and click OK.
  2. In the Solution Explorer on the right hand side, click the arrow to open MainActivity.cs to see MainActivity and open it by double clicking on it. This opens up the C# code.
  3. In the Solution Explorer, under Resources, Layout, double-click Main.axml.
  4. On the left hand side open the Toolbox. As an illustration, add a textblock. Under Toolbox you can search for “tex” to find the textblock. Click it and drag it to the interface diagram in the center of the screen. Under Properties on the left side, go to text. It says Medium Text. Change it to something like “You clicked 0 times.”
  5. Go to the bottom of the center part of the screen to see two tabs and click on Source. Here is your XAML source code. Notice that you see the text you entered in the source code, however, you do not see the Hello text. You just have a reference to a text resource. android:text=”@string/Hello”. In the Solution Explorer in the right side, look at Resources, values, Strings.xml. In there you will find the actual text. In the top section of the Source code add the following just under android:layout_height=”match_parent”: android:padding=”8dp”. 8dp means eight density pixels. Switch back over to the designer view to notice that it has changed and there is now a border of unused space around the content. That looks better. We want to be sure to update the label and not the button. To accomplish that, do the next step.
  6. Go back to the MainActivity. Change the code as shown below. This is the “code-behind”.

Below is the C# code you get when you first create an Android project, as you did in step two above.. I have adjusted it slightly by adding and removing some white space.

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace App2
{
    [Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            button.Click += delegate {
                button.Text = string.Format("{0} clicks!", count++);
            };
        }
    }
}

Here is the updated code below for step 6.

namespace App2
{
    [Activity(Label = "My First Android App", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 0;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            var label FindViewById<TextView>(Resource.Id.textView1);
            button.Click += delegate {
                count++;
                label.Text = $"You clicked {count} times";
            };
        }
    }
}

Android SDK

When I tried step 3 above I got a message saying that The installed Android SDK is too old. Version 24.3.4 or newer is required. Please update to the latest version. I clicked the link provided and the Android SDK Manager window opened. I could not download the latest because of dependencies, so I chose to download version 6.0. Then I went to the Xamarin website and downloaded their installer, called Xamarin Installer. You need to provide your name and email address and a couple of other items.(API 23).

A potential solution to these version headaches is to install Android Studio and let it install the SDK for you.

Leave a comment

Your email address will not be published. Required fields are marked *