Few steps to develop android application using xamarin in c#

Page 1

15 minutes guide to build Android mobile applications using c# 

About Xamarin Studio

Xamarin.Android architecture

Xamarin Studio IDE

Nice to know

Sample app: Layout design

Sample app: Activity code

Sample app creation demo


Submitted By: Mobilepundits 1. Bounce Right In Welcome to Xamarin! Once the setting up complete, follow these simple instructions to write and test your first Android C# code in minutes!

1.1. Running the Sample Application First, if we want to run our application we have to open the Tasky sample application, which can be open in either Xamarin Studio or Visual Studio. If we want to track our task, tasky is a simple To-Do application that everyone can use. It’s a cross-platform sample that shares its core code between Android and iOS and illustrates good cross-platform architectural design including data access, business logic, and native-UI elements.

Check that the Debug configuration is selected in the toolbar and choose Run → Debug from the menu or press Command + return to start debugging with the Android Emulator. Before an emulator first we have to choose an Android Virtual Device (AVD) to use, as shown in these screens:


Once the emulator configured successfully. You can start to add some tasks to see how the application works - touch the Add Task button to begin!

Before looking at the code more closely, let’s revisit the solution and application architecture. There are two projects, which help facilitate a cross-platform solution when combined with Xamarin.Android: TaskyAndroid - An Android application project containing the user-interface and application layer for Android devices. Tasky.Core.Android - A Core library project which contains platform-independent code. This code is shared between different device platforms such as Android, iOS, and Windows Phone. Each platform has a different .csproj file (e.g. Tasky.Core.Android, Tasky.Core.iOS) but share the same C# code files.

1.2. Understanding the planning of Tasky application The planning and project flow of the Tasky application is display in the following diagram:


The application is divergent into different Layers (each layer is represented by a namespace), and each layer has a different purpose:

User Interface - The screens, controls and data presentation code. In Xamarin.Android these classes are wrappers around the Android SDK. The user interface that you build looks, feel and perform like a native Java application.

App Layer - The application layer is Custom classes to bind the business layer to the user interface, typically requiring platform specific features.

Business Layer - This layer play role like Business object classes and business logic. Data Access Layer - Data access layer provide Abstraction between the business logic and the data layer.

Data Layer - Low-level data persistence and retrieval; the Tasky sample uses a SQLite database binding.

2. Transform Tasky


As it sits, Tasky lacks a basic feature of any decent task application: the ability to mark a task complete. I am going to add that functionality by making the following modifications: > Implement a new property on the Task object called done. > Add an instrument in the UI to PERMIT users to mark a task as completed. > switch the home screen to Show whether a task is completed or not. > I am also going to clean up the design of the application a little bit adjustment in the navigation bar style. After I have made all of these changes, here is how Tasky will look:

2.1 Implement an advanced resource on the Task class Unfolded the Task.cs file in Tasky.Core.Android and notice the class has the following property: public bool Done {get; set ;} Tasky also is very helpful for to store data using ADO.NET and retrieve data using the built-in SQLite database engine. All data operations can be performed with familiar SQL syntax and ADO.NET classes. Here's an example of how ADO Connection and Command objects can be used to access data stored in SQLite on an Android device:


2.2. Add a parallel property to the Task Details screen in the Application Layer Now that I have modified my Task object, let’s modify the user interface to allow users to mark them as complete. Screens in Xamarin.Android are basically defined by an XML layout file (with the .axml suffix) and one or more C# subclasses of Android.App.Activity that load that layout. What I have to do, need to modify both the AXML and the C# to add the done property to our user interface. The Task Details layout is defined in the TaskDetails.axml file in the Resources/Layout folder. It already contains controls for the Task Name and Notes text inputs and labels, plus two buttons to Save or Delete the Task. To display the Task’s completion status we will add a Checkbox to this layout with the following XML after the Notes Text control (which includes re-defining the layout_below property of both buttons):


2.3. Sync changes made in the user interface back to the business object so they are saved Displaying the Done checkbox control is only half of the data binding job - we must also load the current task’s done status to set the control, and save the value if it changes. The Task Details Activity subclass is defined in the TaskDetailsScreen.cs file in the Screens folder. The following changes are required to hook up the checkbox to the business object: Define a new field in the class: Checkbox doneCheckbox; Find the checkbox control and set its value in the On Create method: doneCheckbox = FindViewById<CheckBox>(Resource.Id.chkDone); doneCheckbox.Checked = task.Done; Retrieve the value before persisting in the Save method: task.Done = doneCheckbox.Checked; the updated task business object is then saved to the database via the Business Layer’s Task Manager Class. This completes the changes to the Task Details screen - it will now display and save the new property we added to our business object.

2.4. Alter the Home screen so that the done status is displayed in the list The final step is to display the completion status of each task on the home screen.


Each row in the task list is rendered via calls to the GetView method of TaskListAdapter.cs file in the Adapters folder. We need to alter the code to use a CheckedTextView and then set the checked property. To do this replaces the custom TaskListItem layout with the SimpleListItemCheck layout:

As I have changed the layout, delete the code describing the old Task List and replace it with code that matches the CheckedTextView properties view.SetText (item. Name==""?"<new task>":item. Name, TextView.BufferType.Normal); view.Checked = item.Done;

2.5. Apply some styling to personalize the display of the application So in a moment Tasky is a bit more helpful, but let’s tang it up a bit by reform the style of the application bar. Let’s change its color to a nice blue. To do this, add the following code to the HomeScreen.cs and TaskDetailsScreen.cs files in the OnCreate method, make sure that it goes after the SetContentView method:

The Android.Views namespace will need to be included in this file; you can easily add it by right-clicking on the code and resolve using Android.Views There are lots more styling changes you can make to an Android application, including hiding the application bar altogether and replacing it with your own custom view. Now the user interface looks like this:


Awesome, I have now played with my first application in Xamarin.Android. I have seen Xamarin Studio in action, and I built and tested an application in the emulator.

3. Next Steps Great job! You're on your way to enhancing a breathtaking mobile application developer.

Conclusion: This guide teaches you basic setup of Xamarin installation on any android device. Once you setup all these successfully you can move to design an advance android applications using Xamarin. The Applications like calendar app, digital clock, calculator and many more applications which you want to use in your daily life.

About MobilePundits MobilePundits partners with global enterprises to drive their innovation-led growth. That's why Deloitte awarded MobilePundits .Among the top 100 most innovative companies. As a leading provider of nextgeneration consulting, technology and outsourcing solutions, MobilePundits helps clients across the world. Visit www.mobilepundits.com and see how MobilePundits, offers its best outsource and development services.

For more information, contact marketing@mobilepundits.com


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.