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>
- <?xml version="1.0" encoding="utf-8" ?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- tools:context=".MainActivity"
- tools:deviceIds="wear_round">
- <TextView android:text="Xamarin.Android Messaging Survey Sample"
- android:id="@+id/txtWelcome"
- 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" />
- <LinearLayout android:orientation="vertical"
- android:id="@+id/layoutMessaging"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <include layout="@layout/messageview" />
- </LinearLayout>
- </LinearLayout>
Adding the Functionality
Now create a new class file and name it as "MessageScreen". This file serves as the code behind for the survey logic. Here's the code block below:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Android.Views;
- using Android.Widget;
- namespace Xamarin.Messaging
- {
- public partial class MainActivity
- {
- MessagingRunnableTask _MRT;
- bool _isQuestionAvailable = true;
- bool _isMessageAQuestion = true;
- Messaging _message;
- Queue<Message> _availableMessages;
- TextView _txtQuestion;
- Button _btnYes;
- Button _btnNo;
- Button _btnOk;
- LinearLayout _llQuestionButtons;
- ImageView _imgMessageIcon;
- Messaging.MessageIconType _messageIconType;
- Messaging.MessageType _messageType;
- public void DisplayMessage()
- {
- InitializeObjects();
- _availableMessages = _message.GetMessages();
- _llQuestionButtons.Visibility = ViewStates.Visible;
- _isQuestionAvailable = true;
- ShowQuestion();
- RunOnUiThread(WireQuestionButtons);
- }
- void AnswerNo_Click(object sender, EventArgs e) { ShowNextQuestion(); }
- void AnswerYes_Click(object sender, EventArgs e) { ShowNextQuestion(); }
- void AnswerOk_Click(object sender, EventArgs e) { ShowNextQuestion(); }
- void WireQuestionButtons()
- {
- _btnNo.Click -= AnswerNo_Click;
- _btnNo.Click += AnswerNo_Click;
- _btnYes.Click -= AnswerYes_Click;
- _btnYes.Click += AnswerYes_Click;
- _btnOk.Click -= AnswerOk_Click;
- _btnOk.Click += AnswerOk_Click;
- }
- void InitializeObjects()
- {
- _btnNo = FindViewById<Button>(Resource.Id.btnNo);
- _btnYes = FindViewById<Button>(Resource.Id.btnYes);
- _btnOk = FindViewById<Button>(Resource.Id.btnOk);
- _txtQuestion = FindViewById<TextView>(Resource.Id.txtQuestion);
- _llQuestionButtons = FindViewById<LinearLayout>(Resource.Id.linearLayoutQuestionButtons);
- _imgMessageIcon = FindViewById<ImageView>(Resource.Id.imgHolder);
- _MRT = new MessagingRunnableTask();
- _message = new Messaging();
- }
- void ToogleButtons()
- {
- if (_isMessageAQuestion)
- {
- _btnNo.Visibility = ViewStates.Visible;
- _btnYes.Visibility = ViewStates.Visible;
- _btnOk.Visibility = ViewStates.Gone;
- }
- else
- {
- _btnNo.Visibility = ViewStates.Gone;
- _btnYes.Visibility = ViewStates.Gone;
- _btnOk.Visibility = ViewStates.Visible;
- }
- }
- void ToggleIcon(Messaging.MessageIconType iconType)
- {
- switch (iconType)
- {
- case Messaging.MessageIconType.None:
- {
- _imgMessageIcon.SetImageResource(0);
- break;
- }
- case Messaging.MessageIconType.Question:
- {
- _imgMessageIcon.SetImageResource(Resource.Drawable.imgMessagingQuestion);
- break;
- }
- }
- }
- void ToggleButtonText(Messaging.MessageType messageType)
- {
- switch (messageType)
- {
- case Messaging.MessageType.Information:
- {
- _btnOk.Text = "OK";
- break;
- }
- }
- }
- void ShowQuestion()
- {
- if (_availableMessages.Any())
- {
- var m = _availableMessages.First();
- _txtQuestion.Text = m.MessageText;
- _isMessageAQuestion = m.IsQuestion;
- _messageIconType = (Messaging.MessageIconType)m.IconID;
- _messageType = (Messaging.MessageType)m.MessageTypeID;
- _message.Update(m);//deque
- if (!_availableMessages.Any())
- _isQuestionAvailable = false;
- ToggleIcon(_messageIconType);
- ToogleButtons();
- ToggleButtonText(_messageType);
- }
- else
- _isQuestionAvailable = false;
- }
- void ShowNextQuestion()
- {
- if (_isQuestionAvailable)
- ShowQuestion();
- else
- {
- _btnNo.Visibility = ViewStates.Gone;
- _btnYes.Visibility = ViewStates.Gone;
- _llQuestionButtons.Visibility = ViewStates.Gone;
- _txtQuestion.Text = "Saving...";
- SaveAndBackToPreviousView(1000);
- }
- }
- void SaveAndBackToPreviousView(int waitInMilliseconds)
- {
- _MRT.ProcessMessageTask(waitInMilliseconds, true);
- }
- }
- }
Next is open up "MainActivity" class and update the code with the follwing:
- using System;
- using Android.App;
- using Android.Content;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
- namespace Xamarin.Messaging
- {
- [Activity(Label = "Xamarin.Messaging", MainLauncher = true, Icon = "@drawable/icon")]
- public partial class MainActivity : Activity
- {
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.RectangleMain);
- RegisterSwitchViewtReciever();
- MessagingRunnableTask task = new MessagingRunnableTask();
- task.ProcessMessageTask(5000, false);
- }
- public void SetView(string viewName)
- {
- Intent intent = new Intent("SwitchView");
- intent.PutExtra("view", viewName);
- Application.Context.SendBroadcast(intent);
- }
- public void Home()
- {
- FindViewById<TextView>(Resource.Id.txtWelcome).Visibility = ViewStates.Visible;
- FindViewById<LinearLayout>(Resource.Id.layoutMessaging).Visibility = ViewStates.Gone;
- }
- SwitchViewReciever switchViewReciever;
- void RegisterSwitchViewtReciever()
- {
- switchViewReciever = new SwitchViewReciever();
- switchViewReciever.ActionOnRecieve = SwitchView;
- IntentFilter filter = new IntentFilter("SwitchView");
- RegisterReceiver(switchViewReciever, filter);
- }
- public void SwitchView(string view)
- {
- FindViewById<TextView>(Resource.Id.txtWelcome).Visibility = view == "Home" ? ViewStates.Visible : ViewStates.Gone;
- FindViewById<LinearLayout>(Resource.Id.layoutMessaging).Visibility = view == "Survey" ? ViewStates.Visible : ViewStates.Gone;
- Action layoutAction;
- switch (view)
- {
- case "Survey":
- layoutAction = DisplayMessage;
- break;
- default:
- layoutAction = Home;
- break;
- }
- layoutAction.Invoke();
- }
- }
- public class SwitchViewReciever : BroadcastReceiver
- {
- public Action<String> ActionOnRecieve;
- public override void OnReceive(Context context, Intent intent)
- {
- ActionOnRecieve.Invoke(intent.GetStringExtra("view") ?? "Home");
- }
- }
- }
The Output
Running the app will display like this in a real smart watch device.
On initial load

Displays the question after 5 seconds,

After all questions have been shown it will then show a “Thank You” message and return to the home view.
Thanks for reading and I hope someone finds this article useful.

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!