Hello World - Application:
In Our previous tutorials, we discuss Xamarin, its features, and we discuss Emulators. Make sure you came through all of the previous tutorials because these all are Important to move on.
So, let's start to create our first Application. Follow the given steps to create your first Android application.
“
NOTE: In this tutorial, we will learn to create Android Application, and will learn, how to use Emulator. In next tutorials, we will discuss, how did we create this application and we will discuss each file of the project in detail.
Getting Started with Xamarin Window will Open, from there, you can get some tutorials, these tutorials can help You to learn Xamarin development.
In main.axml file, type the following code to build the layout.
<?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:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="@string/HelloWorld"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<Button
android:text="@string/buttonClick"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button1" />
</LinearLayout>
In String.xml file, we need to store information and values about the Button the we created in Main.axml file. copy the given code and paste it in your string.xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloWorld</string>
<string name="HelloWorld">Hello World- Its Xamarin:</string>
<string name="buttonClick">Click ME:</string>
</resources>
This is our main file. we will discuss it in next tutorials, for now, just copy the given code and replace it with your code in MainActivity.cs file.
using Android.App;
using Android.Widget;
using Android.OS;
namespace HelloWorld
{
[Activity(Label = "HelloWorld", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.button1);
button.Click += delegate
{
button.Text = "Congrats - Your First App is ready";
};
}
}
}
Now Our first program is ready, we just need to build the Program and test it in our emulator. follow the given steps to build it.
Congrats, you have successfully created Your First Android Application using C# and Xamarin, inside the Visual Studio. In our Next Tutorials, we will discuss each part of this program. we will explore the project files and will discuss this "Hello World" Application in detail.