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.
Step 2
We need to make it ready for development. For every project, you must understand the anatomy of the file in your project.
Here, the properties and reference are the same as in any C# project. The properties consist of the assembly file and manifest file.
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.
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.
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 Defualt text from the *.cs file and *.axml file.
-
-
- Button button = FindViewById<Button>(Resource.Id.MyButton);
-
- 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.
Step 4
Now, add a Button control from the Toolbox on the Main layout.
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.
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:
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
-
- SetContentView(Resource.Layout.Main);
-
-
- Button myButton = FindViewById<Button>(Resource.Id.button1);
- myButton.Click += myButton_Click;
- }
-
- void myButton_Click(object sender, EventArgs e)
- {
-
- Toast.MakeText(this, "C# Corner : This is Xam App", ToastLength.Long).Show();
-
- }
- }
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 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.
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:
- void myButton_Click(object sender, EventArgs e)
- {
-
- Toast.MakeText(this, "C# Corner : This is Xam App", ToastLength.Long).Show();
-
- }
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:
Conclusion
In this application, we have tried to create a basic demonstration of Android App using Xamarin. For any confusion you may refer to me or try to get it from the attached solution file.