Close
Topic :Create First Android Application - Main.axml file. | Strings.xml file. | MainActivity.css file.

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.



First-AndroidProject-step-1: infobrother
  • #1: Steps to Open New Project

    1 » Open your visual studio, and from top menu, open file > New > Project.



First-AndroidProject-step-2: infobrother
  • #2: Steps to Create Android Project

    1 » In New Project Window, Open Visual C# > Android.
    2 » Select Black App (Android).
    3 » Give it a Proper Name, just like "Hello World"
    4 » Browse Your Location, where you want to store your project. Its better to let it be default.
    5 » Click Ok for the New Android Project to load.



Getting Started with Xamarin Window will Open, from there, you can get some tutorials, these tutorials can help You to learn Xamarin development.



First-AndroidProject-step-3: infobrother
  • #3: Steps to Edit Main.axml File:

    1 » In Solution Explorer Tab, Open Resources > Layout > Main.axml.
    2 » Using Toolbox, Drag and drop Button in Designer Window.
    3 » Drag and Drop TextView in Designer Window.
    4 » Click at Source button to get into the XML File.


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>



First-AndroidProject-step-4: infobrother
  • #4: Steps to Edit Strings.xml File:

    1 » In Solution Explorer tab, Open Resources > values > strings.xml


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>



First-AndroidProject-step-5: infobrother
  • #5: Steps to Edit MainActivity.cs File:

    1 » In Solution Explorer tab, Open MainActivity.css file to edit.


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";
            };
        }
    }
}






I am using Android Virtual Device (AVD) and i had created my own. you Can Use some Pre-configured devices or you Can Create your Own Device. just follow the link to know how to Use pre-configured devices or how to create custom virtual device.




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.



First-AndroidProject-step-6: infobrother
  • #6: Steps to Build Application:

    1 » Click Emulator button to start building Application.
    2 » You can check here the progress of Application. if there is Any error, you will find here. It will take a little time to get the output in Emulator.
    3 » Finally, you will see the Output in your Emulator device.
    4 » Click the button, and you will see that text of button will change.



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.





















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement