Thursday, April 18, 2013

Creating first Android app using C# / Dot42

First, you have to download the Dot42 setup from https://www.dot42.com/download.aspx.  The community license is free for a single developer and free apps can be developed using the community license.  For commercial purpose, you have to purchase the professional license.

Install the Dot42Setup.exe and follow the steps to complete the installation:
























Now, open the Visual Studio 2010 and create a new project using Dot42 template.  Select dot42 Applicatino project and provide a application name.



Once you click on OK, you have to select the Target framework version along with the certificate.  While creating the application for the first time, you have to select the  icon and create a new certificate.












Once you click on Ok, the new project gets created with two files (MainActivity.cs and MainLayout.xml which is the layout xml file).






















By default, a TextView is created with the default Hello World as the text.  Now, let us add a new button and on click of the button, we can show an dialog.  First, we have to add the button in the Layout xml file.


  <Button
      android:id="@+id/button"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Show Alert"/>

In the OnCreate method, we have to identify the button in the layout file first and add event handler to it.

        var button = FindViewById<Button>(R.Ids.button);

        button.Click += ShowOnClick;

Now, we have to create the method ShowOnClick and write logic to show the alert dialog.


            var builder = new AlertDialog.Builder(this);
            builder.SetMessage("Test alert dialog !");
            var dialog = builder.Create();
            dialog.Show();

Now, the MainActivity.cs file looks like below:














Okay, it's time now to debug and deploy the application into the mobile device.  I'm using HTC explorer mobile for deploying this test application.  Since HTC Explorer runs on Andorid 2.3 version, I'm changing the target version to 2.3 from Project -> Properties -> Android -> Android Version -> 2.1.

I have installed HTC Sync application in my local machine and connected my mobile using USB port / HTC Sync option.

Now, press F5 to deploy the application.  Once you press F5, the device connected to the USB port gets listed in the list.  I'm selecting the HTC mobile and clicking on Ok.




























The application is deployed into the mobile device successfully.

Clicking on the button on the application, generates the alert dialog like below.














That's it :) .... I have created my first Android app in less than 10 minutes.

No comments:

Post a Comment