- Xamarin - Deploying Your App
- Xamarin - Multiscreen App
- Xamarin - Andriod Views
- Xamarin - Gallery
- Xamarin - Android Dialogs
- Xamarin - Android Widgets
- Xamarin - Layouts
- Xamarin - Menus
- Xamarin - Building the App GUI
- Xamarin - Permissions
- Xamarin - Android Activity Lifecycle
- Xamarin - Android Resources
- Xamarin - Application Manifest
- Xamarin - First Application
- Xamarin - Installation
- Xamarin - Home
Xamarin Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Xamarin - First Apppcation
In this chapter, we will see how to create a small Android apppcation using Xamarin.
Hello Xamarin! Apppcation
First of all, start a new instance of Visual Studio and go to File → New → Project.
On the Menu dialog box that appears, go to Templates → Visual C# → Android → Blank App (Android).
Give an appropriate name for your apppcation. In our case, we name it “helloWorld” and save it in the default location provided. Next, cpck the OK button for the new “helloXamarin” project to load.
On the solution, open Resources → layout → Main.axml file. Switch from Design View and go to the Source file and type the following pnes of code to build your app.
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:background = "#d3d3d3" android:layout_width = "fill_parent" android:layout_height = "fill_parent"> <TextView android:text = "@string/HelloXamarin" android:textAppearance = "?android:attr/textAppearanceLarge" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/textView2" android:textColor = "@android:color/black" /> </LinearLayout>
In the above code, we have created a new Android textview. Next, open the folder values and double-cpck Strings.xml to open it. Here, we are going to store information and values about the button created above.
<?xml version = "1.0" encoding = "utf-8"?> <resources> <string name = "HelloXamarin">Hello World, I am Xamarin!</string> <string name = "ApppcationName">helloWorld</string> </resources>
Open MainActivity.cs file and replace the existing code with the following pnes of code.
using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; namespace HelloXamarin { pubpc class MainActivity : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); } } }
Save the apppcation. Build and then run it to display the created app in an Android Emulator.
If you do not have an Android Emulator, then follow the steps given in the next section to create one.
Setting Up an Android Emulator
On your Visual Studio menu, go to Tools → Android → Android Emulator Manager. On the pop-up window that appears, cpck the Create button. It will display the following screen.
On the above screen, supply the AVD name you want. Select a device that is appropriate for your display, e.g., Nexus 4” display. Select your target platform. It is always advisable to test on a minimum target platform, e.g., API 10 Android 2.3 (Gingerbread) so as to ensure your App works across all Android platforms.
Fill in the rest of the fields and cpck the OK button. Your emulator is now ready. You can select it from the pst of existing Android Virtual Devices and then cpck Start to launch it.
Modifying HelloXamarin App
In this section, we will modify our project and create a button which will display text upon cpck. Open main.axml and switch to source view. After our textview that we created, we will add a button as shown below.
<Button android:id = "@+id/MyButton" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/ButtonCpck" />
After adding a button, our full code will look pke this −
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent"> <TextView android:text = "@string/HelloXamarin" android:textAppearance = "?android:attr/textAppearanceLarge" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/textView2" /> <Button android:id = "@+id/MyButton" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/ButtonCpck" /> </LinearLayout>
Next, we register our button values in the strings.xml file.
<string name = "ButtonCpck">Cpck Me!</string>
After adding our button in the strings.xml file, we will open MainActivity.cs file to add an action for our button when it is cpcked, as shown in the following code.
using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; namespace HelloXamarin { [Activity(Label = "HelloXamarin", MainLauncher = true, Icon = "@drawable/icon")] pubpc class MainActivity : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); Button button = FindViewById<Button>(Resource.Id.MyButton); button.Cpck += delegate { button.Text = "Hello world I am your first App"; }; } } }
Next, build and run your apppcation.
After cpcking on the button, you will get the following output −
Advertisements