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:
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/
Xamarin requires some fixed Layout to be followed by each AXML, so that its residing controls will be arranged properly.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
EditText UserPwdText = FindViewById
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(); */
};
}
}Conclusion
At last I hope all the details are covered properly and please do post if any technical issues or suggestions are their.