Android App Development Using C# and Xamarin


In this article we first try to create proper Xamarin.Android development environment by installing all the required components on our machine and then build our first Xamarin.Android application in C# language using Visual Studio.

Introduction


Xamarin lets us write shared user-interface code in C# language. It supports XAML (Extensible Application Markup Language) which maps the code to native controls on android platform.

Installing steps for Xamarin.Android on Windows Machine


The following steps are required to install Xamarin.Android:

  • 1. Install the Java SDK (JDK)

  • 2. Install the Android SDK

  • 3. Install GTK#

  • 4. Install Xamarin Studio for Windows (Optional)

  • 5. Install Xamarin 3 (Includes Xamarin.Android)

  • 6. Configure Xamarin.Android

    • Navigate to Visual Studio Tools=> Options=> Xamarin=> Android Settings=>

    • Then set all the 3 paths viz Java Development Kit Location, Android SDK Location and Android NDK Location where you had installed them.

Creating an Android App


I will suggest you all that you first go through the Getting Started documents on the Xamarin website: http://developer.xamarin.com/guides/android/getting_started/

  • Now select File => New Project from the visual studio menu, and in the New Project dialog, from the left select Visual C# and then Android, and from the template list in the center select Blank App (Android).

  • Give some name like DemoApp.

  • Right click on DemoApp.Droid Project => Add => New Item => Android Layout (Which will be the AXML file, name: Login.axml)

  • Here are some of the basic controls information which I had used then in the below code:
    Xamarin requires some fixed Layout to be followed by each AXML, so that its residing controls will be arranged properly.

    • LinearLayout: By default layout. Controls are added row wise.

    • TableLayout: Table like structure. Needs every control to be present inside <-TableRow><-/TableRow>.

    • TextView= For Label

    • EditText = For Textbox

    • Button = For Button

Login.axml code:



<-?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"
android:minWidth="25px"
android:minHeight="25px"
android:background="#E6E6E6">
<-EditText xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="30.0dp"
android:id="@+id/editText1"
android:inputType="text"
android:hint="Username"
android:drawableRight="@drawable/user2"
android:layout_marginRight="0.0dp"
android:layout_marginLeft="0.0dp"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:textColor="@android:color/background_dark"
android:background="@android:color/white"
android:cursorVisible="true" />
<-EditText
android:layout_width="match_parent"
android:layout_height="30.0dp"
android:id="@+id/editText2"
android:inputType="textPassword"
android:hint="Password"
android:drawableRight="@drawable/lock2"
android:layout_marginRight="0.0dp"
android:layout_marginLeft="0.0dp"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:textColor="@android:color/background_dark"
android:background="@android:color/white"
android:cursorVisible="true" />
<-Button
android:id="@+id/Submit"
android:layout_width="match_parent"
android:layout_height="30.0dp"
android:text="Sign In"
android:focusable="true"
android:typeface="serif"
android:layout_marginRight="0.0dp"
android:layout_marginLeft="0.0dp"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:textStyle="normal"
android:capitalize="none" />
<-/LinearLayout>

Now right click on DemoApp.Droid project => Add => New Item => Activity (Which will be the C# code file, name: MainActivity.cs)

MainActivity.cs code:



[Activity(Label = "DemoTitle or Main Screen", MainLauncher = true, Icon = "@drawable/icon")]
/*Here on the very first line, you will see the MainLauncher = true property, which describes that current form is a start form of this app or not. If it is equal to true then current form will be displayed first when we run the app.*/
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle) /*Will be same like Form_Load() event */
{
base.OnCreate(bundle);
/* Following line is responsible for mapping this .cs file with a layout. So we are setting our view from the "Login" layout resource to this MainActivity.cs */
SetContentView(Resource.Layout.Login);
EditText UserIdText = FindViewById(Resource.Id.editText1);
EditText UserPwdText = FindViewById(Resource.Id.editText2);
var button1 = FindViewById<-Button>(Resource.Id.Submit);
button1.Click += (s, e) =>
{
if (UserIdText.Text.Trim().ToLower() == "admin" && UserPwdText.Text.Trim().ToLower() == "admin")
{
/* If UserId and UserPwd are "admin" then redirect to 2nd form or layout */
var intent = new Intent(this, typeof(ActivityNumber2)); /* Provide activity name here where you want to redirect the user */
StartActivity(intent);/* Same as Form.Show(); */
}
else
Toast.MakeText(this, "Login failed!" , ToastLength.Long); /* Same as MessageBox.Show(); */
};
}
}

  • Now build the app, which will create .apk file for android and .exe files for IOS and Windows.

  • Now for running this app, Navigate to Tools=> Android=> Android Emulator Manager.

  • Now create new android virtual device where you will test this app and then deploy it on real phones and then click on Start Button.

  • Now this emulator is shown next to your Run/Debug button and whenever you run this app, it will be executed on emulator.


Conclusion


At last I hope all the details are covered properly and please do post if any technical issues or suggestions are their.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: