Sponsored Ads

Programming Apps

Sponsored Links

Creating a Hello World with Xamarin using Visual Studio 2015

 

Short hand: File > New Project > Templates > Visual C# > Android >Blank App (Android)

 
 

Click Ok to begin

The first window you may see is the GettingStarted.Xamarin you can close this window.
Next double click MainActivity.cs in the Suluation Explorer this is where you’ll add your code.

 
 

Next open the designer window, to do this expand Resources > layout and double click on Main.xmal

 
 

At the bottom left side of the screen you have your Designer view and Source view. Clicking on the Source view switches view to AXML coding

 
 

Let’s start designing our Hello World by setting the view to Designer.

 
 

Open your Toolbox (View > Toolbox) and search for the word button in the search field

 
 

Next drag and drop the button icon onto the designer, when the button has been added to your designer repeat this action by doing a search in the Toolbox for the word TextView then drag and drop the TextView to the designer.

 
 

Now switch to Source view to modify the source code. Let’s give our button a new name was well as a linkage ID.


Change the following code from:

<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1" />

To

<Button
android:text="Tap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tapButton" />

What we have done is given the button a new name Tap as well as given the button a ID that we can use to recognised it as an object.

Let’s do the same for the text field by changing the following code from:

<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"

To

<TextView
android:text=""
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center" />
android:id="@+id/TextArea"

 

Add C# code
In order to view the newly added content you’ll need to turn on the view content in the MainActivity by un commenting the setContentView from:

 

// SetContentView (Resource.Layout.Main);

To

SetContentView (Resource.Layout.Main);

 

Coding the button
Switch to MainActivity.cs and create a new instance of the tapButton and textArea type in the following code:

 

Button tapButton = FindViewById<Button>(Resource.Id.tapButton);
TextView textArea = FindViewById<TextView>(Resource.Id.TextArea);

 

Now make the button active by adding the following code:

 

tapButton.Click += (object sender, System.EventArgs e) =>
{
//Add your button code here
tapButton.Text = "You've tap this button";
textArea.Text = "Hello World!";
};

 

Rebuilding the Solution

 

We just updated some code and need to check if there are any errors on the project that we might have missed.

Click on Build > Rebuild Solution

You can also click on Clean Solution follow by Rebuild Solution, the Rebuild All succeeded should be display at the bottom left side if there are not errors. Otherwise go over what we have done to correct any errors.

 
 

Deploying to an Emulator

 

To test the Android solution click on Project > Hello World Properties…
You’ll need to set the minimum Android version so that it will run on your chosen virtual device. In this scenario the minimum API is set to Android 4.1 (API Level 16 – Jelly Bean). When finished close the window and run the Android Emulator. In this scenario it is running on Android_Accelerated_x86 (Android 6.0 – API 23)

 
 

The End Result

 
 
 
 
 
 
 

Copyright © 2017. All Rights Reserved.
This site uses cookies by using this site you agree to the placement of cookies.