Click here to Skip to main content
15,893,814 members
Articles / Mobile Apps / Android

Android - Stock Market Watch (COINS) in C# using Visual Studio 2010

Rate me:
Please Sign up or sign in to vote.
4.90/5 (102 votes)
24 Mar 2013CPOL23 min read 271.4K   10.1K   289  
Describes the power of C# in building professional application - Stock Market Watch with Charts on Android platform using Mono C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;

using SampleListView.DataLayer;
using SampleListView.stockwebservice;

namespace SampleListView
{
    [Activity(Label = "Add Stock")]
    public class AddStock : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            View titleView = Window.FindViewById(Android.Resource.Id.Title);

            //update the default title
            if (titleView != null)
            {
                IViewParent parent = titleView.Parent;
                if (parent != null && (parent is View))
                {
                    View parentView = (View)parent;
                    parentView.SetBackgroundColor(Color.Rgb(28, 28, 28));
                    parentView.SetMinimumHeight(32);
                    parentView.SetMinimumHeight(32);
                }
            }

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.AddStock);

            EditText textStock = FindViewById<EditText>(Resource.Id.textStock);
            Button btnAddStock = FindViewById<Button>(Resource.Id.buttonAddStock);
            btnAddStock.Click += (sender, e) =>
            {
                if (textStock.Text == string.Empty)
                {
                    //message if no text is entered in script
                    var t = Toast.MakeText(this, "Please eneter Stock Script", ToastLength.Short);
                    t.SetGravity(GravityFlags.Center, 0, 0);
                    t.Show();

                    textStock.Focusable = true;
                    return;
                }

                //check script from webservice to see, if it is a valid script
                if (doCheckStockScript(textStock.Text.Trim()))
                {
                    //if valid, add script to sqlite database
                    bool ret = AddStockScript(textStock.Text.Trim());
                    if (!ret)
                    {
                        textStock.Focusable = true;
                        return;
                    }
                    else
                    {
                        textStock.Text = "";
                        textStock.Focusable = true;

                        StartActivity(typeof(MarketWatch));
                    }
                }
                else
                {
                    //message if no text entered is not a valid script
                    var t = Toast.MakeText(this, "You have eneterd invalis Stock Script. Please eneter a valid Stock Script", ToastLength.Short);
                    t.SetGravity(GravityFlags.Center, 0, 0);
                    t.Show();

                    textStock.Focusable = true;
                    return;
                }
            };
        }

        private bool doCheckStockScript(string _stockscript)
        {
            //conecting to webservice and get the xml
            string strXML = string.Empty;
            stockwebservice.StockWebservice quoteObject = null;
            try
            {
                quoteObject = new stockwebservice.StockWebservice();
                strXML = quoteObject.StockExists("JPASSOCIAT");
            }
            finally
            {
                quoteObject.Dispose();
                quoteObject = null;
            }

            //if error occurred while connect8ing to web service
            if (strXML.Substring(0, 5) == "error")
            {
                var t = Toast.MakeText(this, "Error connecting to web service. Please check your Internet connection...", ToastLength.Short);
                t.SetGravity(GravityFlags.Center, 0, 0);
                t.Show();
                return false;
            }
            if (strXML.ToLower() == "exception")
            {
                var t = Toast.MakeText(this, "Service not available now. Please try after sometime...", ToastLength.Short);
                t.SetGravity(GravityFlags.Center, 0, 0);
                t.Show();
                return false;
            }


            return true;
        }

        private bool AddStockScript(string _stockscript)
        {
            bool Ok = false;
            StockDatabase.Message = string.Empty;
            Ok = StockDatabase.SaveStockScript(_stockscript);
            if (StockDatabase.Message != string.Empty)
            {
                var t = Toast.MakeText(this, StockDatabase.Message, ToastLength.Short);
                t.SetGravity(GravityFlags.Center, 0, 0);
                t.Show();
            }

            return Ok;
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            menu.Add("Market Watch").SetIcon(Android.Resource.Drawable.IcButtonSpeakNow);
            menu.Add("Add Stock").SetIcon(Resource.Drawable.ic_tab_artists_grey);
            menu.Add("Get Quote").SetIcon(Resource.Drawable.ic_tab_artists_white); ;
            return true;
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.TitleFormatted.ToString())
            {
                case "Market Watch":
                    //MenuItemClicked(item.TitleFormatted.ToString()); break;
                    doOpenMarketWatch(); break;
                case "Add Stock":
                    //MenuItemClicked(item.TitleFormatted.ToString()); break;
                    doOpenAddStock(); break;
                case "Get Quote":
                    //MenuItemClicked(item.TitleFormatted.ToString()); break;
                    doOpenGetStock(); break;
            }
            return base.OnOptionsItemSelected(item);
        }

        void MenuItemClicked(string item)
        {
            Console.WriteLine(item + " option menuitem clicked");
            var t = Toast.MakeText(this, "Options Menu '" + item + "' clicked", ToastLength.Short);
            t.SetGravity(GravityFlags.Center, 0, 0);
            t.Show();
        }

        protected void doOpenMarketWatch()
        {
            StartActivity(typeof(MarketWatch));
        }
        protected void doOpenAddStock()
        {
        }
        protected void doOpenGetStock()
        {
            StartActivity(typeof(AddStock));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect ClarityCX Solutions & Services Pvt. Ltd.
India India
Sandip is a "Software Architect, CEO, Founder at ClarityCX Solutions & Services Private Limited"

He has 24 years of rich experience working in software development in the areas of market research, web, mobile and e-commerce.

Sandip has wide ranging experience in C#, VB.NET, ASP.Net, JQuery, JSON, LINQ, WCF, WPF, NodeJs, MongoDB, AngularJS, Express, Silverlight, SOA, Android, IPhone, IPad and Windows Mobile applications.

Specialized in Hybrid and Native Apps in iOS, Android, Windows Phone, Blackberry, Symbian, Tizen, WebOS.

Sandip can be reached at sandip.net@gmail.com
Tweet: @sandipnascar
FB: https:sandipnascar
View My Page

Comments and Discussions