Having the opportunity to work with android wearables, particularly smart watches, is very exciting as this is getting more popular nowadays. Building mobile and wearable device apps is not as complex as you may think. I admit that at first I was kind of afraid when I was assigned to explore and build apps for mobile and wearable, but using the right tools and technologies makes life easier for me to build those apps and prototypes.
Ermm.. right tools?
Yes, and I was referring to the awesome Xamarin. Xamarin allows you to build cross-platform apps for Android, iOS and Windows and it uses C# as the backend language. They also introduced Xamarin Forms which allows you to easily create native UI layouts that can be shared across Android, iOS, and Windows Phone. For as long as you know C# then creating the logic for your app is easy because you are already familiar with the syntax and most of all the .NET libraries. The only learning curve that you will need to take when transitioning from web to mobile is that you will need to know and understand how Android, iOS and Windows platform works and how each framework interpret stuffs. To trim down a bit of that learning curve, I have decided to use Xamarin and Visual Studio for the following reasons:
- Xamarin is now fully integrated with the latest Visual Studio release (VS 2015 as of this writing).
- Xamarin allows you to build cross-platform apps (iOS, Andriod and Windows app) using C#.
- I am an experienced C# developer.
- I am more familiar with Visual Studio development tools.
- I don't need to learn how to use other frameworks, editors, tools and other programming languages to build native apps.
- I can take advantage of the cool features provided by Xamarin such as cloud testing and app monitoring.
- Xamarin and Visual Studio are quite popular and stable platform for building real world apps.
- Xamarin have their own dedicated support site. So when you encounter any problem during your development, you can easily post your query to their dedicated forums.
I'm writing this article so anyone that might get interested in mobile app development can reference this if they need a simple working app that requires some kind of a questionnaire to collect data from end users. This article will walk you through on building a simple survey questionnaire feature that can be integrated in your android smart watch app. Generally, a survey is a data gathering method that is utilized to collect, analyze and interpret the views of a group of people.
Before you go any further make sure that you have the necessary requirements for your system and your development environment is properly configured. For setting up the development environment in Visual Studio please refer my previous article here: Getting Started With Android Wearable Using Xamarin and Visual Studio
Let's Get Started!
For this demo, I'm going to use Visual Studio 2015 with Xamarin version 4.0. Open Visual Studio 2015 and then create a new project by selecting File > New > Project. It should bring up the following dialog below:
Under Templates select Visual C# > Android > Wear App (Android). Name your app to whatever you like but for the simplicity of this demo I just named it as "Xamarin.Messaging". Click OK to let Visual Studio generate the necessary files for our application. You should now be able to see the following screen:
The first thing we need is to add the needed images for our app. In this example, I've added two images under Resources >Drawable folder as shown below:

The images we’ve added will be used as the background for the Buttons and the other is for the question indicator.
Setting up the Model.
Now add a new class by right-clicking on the solution project and then select Add > Class. Name the class as "Messaging" and add the following properties below:
- using System;
- namespace Xamarin.Messaging
- {
- public class Message
- {
- public int MessageID { get; set; }
- public int MemberID { get; set; }
- public short MessageTypeID { get; set; }
- public short IconID { get; set; }
- public string MessageText { get; set; }
- public bool IsQuestion { get; set; }
- public string SelectedAnswer { get; set; }
- }
- }
The next step is to add a new class file called "Messaging". This file will contain the logic for our survey feature. Here's the code block below:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Android.OS;
- using Java.Util;
- namespace Xamarin.Messaging
- {
- #region MESSAGING LOGIC
- public class Messaging
- {
- public bool isMessageAvailable { get; set; }
- private Queue<Message> Messages { get; set; }
- private List<Message> AnsweredMessages { get; set; }
- private List<Message> RawMessages { get; set; }
- public Messaging()
- {
- this.Messages = new Queue<Message>();
- this.AnsweredMessages = new List<Message>();
- }
- public enum MessageIconType { None = 0, Question }
- public enum MessageType { None = 0, Question, Information }
- public void Add(Message message) { this.Messages.Enqueue(message); }
- public void Update(Message message)
- {
- this.Messages.Dequeue();
- this.AnsweredMessages.Add(message);
- }
- public Queue<Message> GetMessages()
- {
- var messages = GetMessagesByMemberID(1);
- messages.ToList().ForEach(x => this.Add(x));
- return this.Messages;
- }
- private List<Message> GetMessagesByMemberID(int memberID)
- {
- this.RawMessages = new List<Message>();
- this.RawMessages.Add(new Message() { MessageID = 1, MemberID = 1, MessageTypeID = 2, IconID = 1, MessageText = "Do you drink beer?", IsQuestion = true, SelectedAnswer = "" });
- this.RawMessages.Add(new Message() { MessageID = 2, MemberID = 1, MessageTypeID = 2, IconID = 2, MessageText = "Do you like to code?", IsQuestion = true, SelectedAnswer = "" });
- this.RawMessages.Add(new Message() { MessageID = 3, MemberID = 1, MessageTypeID = 3, IconID = 0, MessageText = "Thank you.", IsQuestion = false, SelectedAnswer = "" });
- return this.RawMessages;
- }
- }
- #endregion
- #region RUNNABLE
- public class MessageTimerTask : TimerTask
- {
- MessagingRunnableTask MRT;
- public MessageTimerTask(MessagingRunnableTask mrt) { MRT = mrt; }
- public override void Run()
- {
- MRT._handler.Post(new Action(() =>
- {
- RedirectToView(MRT);
- }));
- }
- void RedirectToView(MessagingRunnableTask mrt)
- {
- MainActivity m = new MainActivity();
- if (mrt._isSurveryDone)
- m.SetView("Home");
- else
- m.SetView("Survey");
- }
- }
- public class MessagingRunnableTask
- {
- MessageTimerTask _showMessageTask;
- Timer _showMessageTimer;
- public Handler _handler;
- public bool _isSurveryDone = false;
- public MessagingRunnableTask() { _handler = new Handler(); }
- public void ProcessMessageTask(int waitInMilliseconds, bool isDone)
- {
- _isSurveryDone = isDone;
- _showMessageTask = new MessageTimerTask(this);
- _showMessageTimer = new Timer();
- _showMessageTimer.Schedule(_showMessageTask, waitInMilliseconds);
- }
- }
- #endregion
- }
You may also have noticed that the file also contains the implementation for runnable. This is to schedule the survey questionnaire within a given period of time. This means that you can control as to when to prompt the users with the survey questions.
Adding the Layout
Now add a new .AXML file under Resources > Layout. Name the layout as "MessageView" as shown in the figure below:

After that switch to "Source" view in the designer and then add the following markup below:
- <?xml version="1.0" encoding="utf-8" ?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_centerHorizontal="true"
- android:maxLines="10"
- android:scrollbars="vertical">
- <ImageView android:layout_centerHorizontal="true"
- android:layout_marginTop="15.0dp"
- android:layout_width="55px"
- android:layout_height="50px"
- android:id="@+id/imgHolder" />
- <ScrollView android:minWidth="25px"
- android:minHeight="25px"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_below="@+id/imgHolder"
- android:id="@+id/scrollView1">
- <TableLayout android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:stretchColumns="1">
- <TextView android:text=""
- android:id="@+id/txtQuestion"
- android:layout_centerHorizontal="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="40dp"
- android:layout_marginBottom="10.0dp"
- android:gravity="center"
- android:layout_below="@+id/imgHolder" />
- <LinearLayout android:orientation="horizontal"
- android:paddingLeft="4.0dip"
- android:paddingTop="5.0dip"
- android:paddingRight="4.0dip"
- android:paddingBottom="1.0dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/txtQuestion"
- android:layout_marginBottom="10.0dp"
- android:gravity="center"
- android:id="@+id/linearLayoutQuestionButtons">
- <Button android:id="@+id/btnYes"
- android:layout_width="114px"
- android:layout_height="46px"
- android:text="YES"
- android:background="@drawable/imgMessagingButton" />
- <Button android:id="@+id/btnNo"
- android:layout_width="114px"
- android:layout_height="46px"
- android:text="NO"
- android:background="@drawable/imgMessagingButton" />
- <Button android:id="@+id/btnOk"
- android:layout_width="114px"
- android:layout_height="46px"
- android:text="OK"
- android:layout_centerHorizontal="true"
- android:background="@drawable/imgMessagingButton"
- android:visibility="gone" />
- </LinearLayout>
- </TableLayout>
- </ScrollView>
- </RelativeLayout>



Santhakumar MunuswamyPosted Jun 10, 2016, 12:11 AM
Thanks for good article
Arvind ChourasiyaPosted May 11, 2016, 1:45 AM
i have seen those all are very useful..it would be more batter if you share something about repository patterns for multiple tables using Sq-lite and how to use repository class in xamarin .thank you
Arvind ChourasiyaPosted May 4, 2016, 10:56 AM
Nice Vincent.share more articals about Xamarin Android
NitinPosted May 4, 2016, 7:38 AM
Thanks for sharing
Bhavik PatelPosted May 3, 2016, 9:40 PM
good share
Afzaal Ahmad ZeeshanPosted May 3, 2016, 12:25 PM
"Do you drink beer?" :D That was a good question. Good article Vincent!
Muhammad Aqib ShehzadPosted May 3, 2016, 11:24 AM
great share
Debasis SahaPosted May 3, 2016, 9:30 AM
Good One..
Vignesh ManiPosted May 3, 2016, 8:36 AM
Nice
Bhuvanesh MohankumarPosted May 3, 2016, 8:15 AM
Good one Vincent
Amit DavePosted May 3, 2016, 6:52 AM
Good share!