Hello World in Xamarin

Introduction

 
So, we have successfully installed all the requirements for Xamarin development. We will now start with a small demonstration of a Mono-Android App.
 
Procedures
 
Step 1
 
Create a Blank Android Project in the New Project section. After the successful creation, you may have this.
 
new Android Project
 
Step 2
 
We need to make it ready for development. For every project, you must understand the anatomy of the file in your project.
 
project resources
 
Here, the properties and reference are the same as in any C# project. The properties consist of the assembly file and manifest file.
 
assemblyInfo
 
While in this journey, you will have open the manifest file because it has the Permission section (in other words Capabilities in Windows Phone Project). Where the Assembly file isn't necessary to be played with. 
 
Next, we have a Reference folder that stores the DLL library files.
 
reference folder
 
As you can see, it has all the required Mono Android DLL and core C# library files.
 
Next, we have an assets folder that stores the external physical resources. For now, we have nothing more than an about file.
 
assets folder
 
Now, the rest is the most used and important part of your project.
 
First, the resources that store the UI layout (*.axml), drawable resources in other words icons and strings.xml (values).
 
In Conventional Java Android, where the layout has been defined in XML and the backend code is in Java (.java) itself.
 
Just like that, Xamrin describes the layout in *.axml file and backend code is in C# (.cs file).
 
That's why, we have a MainActivity.cs file.
 
Step 3
 
Now, we need to remove some Default text from the *.cs file and *.axml file.
  1. // Get our button from the layout resource,  
  2. // and attach an event to it  
  3. Button button = FindViewById<Button>(Resource.Id.MyButton);  
  4.   
  5. button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };  
Remove the entire block from the cs file. And, from Main Activity layout file, delete that button.
 
new xamarin Android Project layout
 
Step 4
 
Now, add a Button control from the Toolbox on the Main layout.
 
Button control
 
And change the Text property to “Click Me” from “Button”. You can do this in one of two ways: 
  • Either go to the Source Tab or change the value of the Button itself.
  • Or, you can do this by changing the value in the Property Windows.
     
    changing value in Property Windows
     
    Property tab
     
We are now done with the layout. Get the backend .cs file, the MainActivity.cs file.
 
Step 5
 
Add some code to the MainActivity.cs file as in the following:
  1. protected override void OnCreate(Bundle bundle)  
  2. {  
  3. base.OnCreate(bundle);  
  4.   
  5. // Set our view from the "main" layout resource  
  6. SetContentView(Resource.Layout.Main);  
  7.   
  8. // Set The Button Linked  
  9. Button myButton = FindViewById<Button>(Resource.Id.button1);  
  10. myButton.Click += myButton_Click;  
  11. }  
  12.   
  13. void myButton_Click(object sender, EventArgs e)  
  14. {  
  15. // This is a Call back Method : myButton.Click  
  16. Toast.MakeText(this"C# Corner : This is Xam App", ToastLength.Long).Show();  
  17.   
  18. }  
  19. }  
Explanation
 
Button myButton =FindViewById<Button>(Resource.Id.button1);
 
Suppose, If I want to use the TextBox1's Text Property in a C# Windows Forms or Windows Phone form, then we just use the TextBox1.Text=”<Whatever>”. But, not in this case.
 
Before using your any control from the CS file, you need to initiate the same instance as you have done in the XAML file.
 
Like, you have designed a Button Control on the Main.axml page, so you need to create a reference of Button Type in the cs file, and link the XAML file. That's what I am doing with the code.
 
Button myButton =FindViewById<Button>(Resource.Id.button1);
 
Let's dissect the code into two parts.
 
Button myButton
 
Here, we are trying to create a reference of Button. And, now:
 
FindViewById<Button>(Resource.Id.button1)
 
FinViewById() is a generic method that is ed by Button Type.
 
The type will remain the same as the type of reference you have made earlier, in other words Button myButton.
 
Now, what is the resource.Id.button1?
 
For that, open the Resource.Designer.cs file. Since this file is responsible for maintaining the unique ID of every resource you have in your project. Either it is your Icons, assets or your every control you have used in your project.
 
Resource Designer file
 
As you can see, a resource is a Parent class. You are then accessing the ID class, and within the Id class you are asking for button1 that returns the unique integer Id.
 
Now:
 
myButton.Click += myButton_Click;
 
Here, we are generating the Click event and calling a callback method. Where we have defined:
  1. void myButton_Click(object sender, EventArgs e)  
  2. {  
  3. // This is a Call back Method : myButton.Click  
  4. Toast.MakeText(this"C# Corner : This is Xam App", ToastLength.Long).Show();  
  5.   
  6. }  
Within this, we are calling the static method MakeText() that needs three arguments. The first is the content that is always "this".
 
The second will be the message that you want to generate and the third will be the kind of message, in other words short or long.
 
Along with it, we have called the Show() method to pop up a message.
 
Output
 
After successful rendering the output will be like:
 
rendering Output of xamarin project
 

Conclusion

 
In this application, we have tried to create a basic demonstration of the Android App using Xamarin. For any confusion, you may refer to me or try to get it from the attached solution file.


Similar Articles