Search in Help for developer site.

Thursday 28 July 2016

Create simple android mobile application using Xamarin and Visual Studio

1. Overview

This article gives an introduction to how to create your first android mobile application using Xamarin. Xamarin allows .NET developers to develop any android/IOS/windows apps using their existing IDE and language like visual studio and C#.
After reading this article you will be able to develop a simple android mobile application using c# with Xamarin which adds any two int number and displays result in an alert box.

2. Prerequisites

 I am assuming that you have already installed Xamarin tool for visual studio and you have installed android SDK and API.

3. Step by step creating your first android mobile application

Let’s start to create android application with visual studio and Xamarin, to create go to
File->New->Project. In the left list under Visual C# select android and then select Blank App (Android) as shown in below screenshot.



Then give any appropriate project name, I’m giving AddTwoNumber as a project name after this click Ok to continue create project

4. Project Structure

When your project has been created, you will see project structure as shown in below screenshot.

  • As you can see layout folder under Resources, to create any layout for android apps.  Now open Main.axml file to see design and source part.
  • To add two number we need two TextBox and 1 button, you can create these control using drag and drop method or using source code. In this article I will use source code to create these controls.

design view

Main.axml Source Code

<?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="fill_parent">
    <EditText
        android:id="@+id/txtNum1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/txtNum2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/MyButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ADD" />
</LinearLayout>

·              Now open MainActivity.cs file to write our C# code to function it.

[Activity(Label = "AddTwoNumber", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            
            FindViewById<Button>(Resource.Id.MyButton).Click += MyButton_Click;
        }

        private void MyButton_Click(object sender, EventArgs e)
        {
            int txtNum1 = Convert.ToInt32(FindViewById<EditText>(Resource.Id.txtNum1).Text);
            int txtNum2 = Convert.ToInt32(FindViewById<EditText>(Resource.Id.txtNum2).Text);
            int result = txtNum1 + txtNum2;
            //showing result in alert message
            new AlertDialog.Builder(this)
                .SetMessage("Result=" + result)
                .SetTitle("Message")
                .Show();

        }
    }
  • That’s it we are done with coding now time to launch our application
  • To run and test this application I will use my android phone  because android emulator runs very slow
  • When you connect your android phone to your PC it will automatically detect your phone it will be shown as per below screenshot


·         Note:-Make sure your phone is connected in USB debugging mode.
·         Now run with F5 and this application will be installed in your mobile automatically.
·         These are the screenshot on my mobile.




5. What do you think

hello readers I hope you liked this post to create a very simple android mobile application using Xamarin tool and visual studio. What do you think about it, let me know your concerns and queries, just write it down in comments and if you like it share it.
  


4 comments:

  1. It's new thing for me. Thank you for sharing.

    ReplyDelete
  2. hello susheel thnx for sharing..but i have one query that where do i get Xamarin tool?..

    ReplyDelete
  3. You can download it from https://www.xamarin.com/download

    ReplyDelete