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:
- Visual Studio: With the .NET Desktop Development and Mobile Development with .NET workloads installed.
- Android SDK: Xamarin requires the Android SDK, which is typically installed with Visual Studio.
- Java Development Kit (JDK): Required by the Android SDK.
You can verify your installation by creating a new Xamarin.Android project in Visual Studio.
Step 1: Creating Your First Xamarin.Android Project
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:
- Properties: Contains project settings and assembly information.
- Resources: This is where you'll find UI layouts, drawables, strings, and styles.
- layout: XML files defining the user interface for your activities.
- drawable: Image assets for your app.
- values: Contains strings, dimensions, and styles.
- Assets: For raw asset files like fonts or custom data.
- <YourActivityName>.cs: The C# code-behind file for your main activity.
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:
SetContentView(Resource.Layout.Main);links the C# code to your XML layout.FindViewById<T>(Resource.Id.your_id);retrieves references to your UI elements.- The
Clickevent handler is attached to the button to execute code when tapped.
Step 5: Running Your Application
With your project set up and code written, you can now run the app:
- Select a Target Device: In the Visual Studio toolbar, choose an Android emulator or a connected Android device from the device dropdown.
- 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.
This is just the beginning! Explore more advanced topics like:
- Handling user input (EditText, CheckBox, etc.)
- Navigation between screens (Activities)
- Working with data (SQLite, SharedPreferences)
- Accessing device features (Camera, GPS)
- Using Material Design components
- Understanding Android lifecycle events
Refer to the MSDN Xamarin documentation for in-depth guides on these subjects.