Xamarin Android Native Development: Getting Started

Welcome to the MSDN tutorial on developing native Android applications using Xamarin. This guide will walk you through the fundamental steps and concepts to build powerful Android apps with C# and .NET.

Prerequisites

Before you begin, ensure you have the following installed:

You can verify your installation by creating a new Xamarin.Android project in Visual Studio.

Step 1: Creating Your First Xamarin.Android Project

1.1 Open Visual Studio.
1.2 Go to File > New > Project...
1.3 In the "Create a new project" dialog, search for "Xamarin.Android". Select the Android App (Xamarin) template.
1.4 Click Next, enter your Project Name (e.g., "MyFirstAndroidApp"), choose a Location, and click Create.
1.5 Select a target framework (e.g., "Xamarin.Android 12.0" or the latest stable version) and click OK.

Visual Studio will create a basic Android project with essential files, including activities, layouts, and resources.

Step 2: Understanding Project Structure

A typical Xamarin.Android project has the following key folders:

Step 3: Designing Your User Interface

The main UI for an Android activity is defined in an XML file within the Resources/layout folder. The default template usually includes Main.axml.

Example: Modifying Main.axml

Open Resources/layout/Main.axml and you can use the visual designer or edit the XML directly. Let's add a button and a text view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello, Xamarin!"
        android:textSize="24sp"
        android:gravity="center"
        android:id="@+id/textViewMessage" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:id="@+id/buttonAction"
        android:layout_marginTop="20dp" />
</LinearLayout>

The id attributes (e.g., @+id/textViewMessage) are crucial for referencing these UI elements in your C# code.

Step 4: Writing C# Code for Interaction

Now, let's make the button do something. Open the C# file for your main activity (e.g., MainActivity.cs).

using Android.App;
using Android.OS;
using Android.Widget;
using Android.Support.V7.App.AppCompatActivity; // Or AndroidX.AppCompat.App.AppCompatActivity
using Android.Util;

namespace MyFirstAndroidApp
{
    [Activity(Label = "MyFirstAndroidApp", MainLauncher = true, Icon = "@mipmap/icon")]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our UI elements from the layout
            TextView textViewMessage = FindViewById<TextView>(Resource.Id.textViewMessage);
            Button buttonAction = FindViewById<Button>(Resource.Id.buttonAction);

            // Attach an event handler to the button
            buttonAction.Click += (sender, e) =>
            {
                textViewMessage.Text = "Button Clicked!";
                Toast.MakeText(this, "Action successful!", ToastLength.Short).Show();
            };
        }
    }
}

In this code:

Step 5: Running Your Application

With your project set up and code written, you can now run the app:

  1. Select a Target Device: In the Visual Studio toolbar, choose an Android emulator or a connected Android device from the device dropdown.
  2. Run the Project: Click the green "Start" button or press F5.

Visual Studio will build the project, deploy it to the selected device/emulator, and launch your application. You should see the "Hello, Xamarin!" text and be able to click the button to change the text and show a toast message.

Next Steps:

This is just the beginning! Explore more advanced topics like:

Refer to the MSDN Xamarin documentation for in-depth guides on these subjects.