Click here to Skip to main content
12,500,915 members (55,104 online)
Click here to Skip to main content

Stats

147.3K views
9.2K downloads
285 bookmarked
Posted

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

, 24 Mar 2013 CPOL
Describes the power of C# in building professional application - Stock Market Watch with Charts on Android platform using Mono C#.
COINSMobile
COINSMobile
Assets
COINSMobile.csproj.user
Properties
Resources
Drawable
GALAXY-Tab-P1000-Product-image-4.png
home_icon.jpg
Icon.png
Icon2.png
icon_activity.png
ic_market_watch.png
ic_quote_add.png
ic_stock_delete.png
ic_stock_get.png
refresh_icon.jpg
splash.jpg
stock.jpg
Layout
AddStock.axml
GetStock.axml
MarketWatch.axml
ShowChart.axml
StockDetails.axml
Values
Screenshots
add_stocks.jpg
get_stock.jpg
main_application.png
option_menu_actionbar.jpg
passing_intent.jpg
Web References
stockwebservice
Reference.map
stockquoteservice.wsdl
COINSMobile.Core
bin
Debug
COINSMobile.Core.dll.mdb
BusinessLayer
DataLayer
obj
Debug
Properties
COINSMobile.suo
SampleListView.suo
COINSMobile.csproj.user
GALAXY-Tab-P1000-Product-image-4.png
home_icon.jpg
Icon.png
Icon2.png
icon_activity.png
ic_market_watch.png
ic_quote_add.png
ic_stock_delete.png
ic_stock_get.png
refresh_icon.jpg
splash.jpg
stock.jpg
AddStock.axml
GetStock.axml
MarketWatch.axml
ShowChart.axml
StockDetails.axml
add_stocks.jpg
get_stock.jpg
main_application.png
option_menu_actionbar.jpg
passing_intent.jpg
Reference.map
stockquoteservice.wsdl
COINSMobile.Core.dll
COINSMobile.Core.dll.mdb
COINSMobile.Core.dll
COINSMobile.suo
SampleListView.suo
bin
Debug
COINSMobile.Core.dll.mdb
COINSMobile.dll.mdb
COINSMobile.csproj.user
home_icon.jpg
Icon.png
Icon2.png
icon_activity.png
ic_market_watch.png
ic_quote_add.png
ic_stock_delete.png
ic_stock_get.png
refresh_icon.jpg
splash.jpg
AddStock.axml
GetStock.axml
MarketWatch.axml
ShowChart.axml
StockDetails.axml
add_stocks.jpg
get_stock.jpg
main_application.png
option_menu_actionbar.jpg
passing_intent.jpg
Reference.map
stockquoteservice.wsdl
COINSMobile.Core.dll.mdb
COINSMobile.suo
SampleListView.suo
COINSMobile.Core.dll
COINSMobile.Core.dll.mdb
COINSMobile.dll
COINSMobile.dll.mdb
COINSMobile.csproj.user
home_icon.jpg
Icon.png
Icon2.png
icon_activity.png
ic_market_watch.png
ic_quote_add.png
ic_stock_delete.png
ic_stock_get.png
refresh_icon.jpg
splash.jpg
AddStock.axml
GetStock.axml
MarketWatch.axml
ShowChart.axml
StockDetails.axml
add_stocks.jpg
get_stock.jpg
main_application.png
option_menu_actionbar.jpg
passing_intent.jpg
Reference.map
stockquoteservice.wsdl
COINSMobile.Core.dll
COINSMobile.Core.dll.mdb
COINSMobile.Core.dll
COINSMobile.suo
SampleListView.suo
FinanceWebService
App_Code
App_Data
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 System.Xml;
using System.Threading;
using Android.Database.Sqlite;
using COINSMobile.Core.BusinessLayer;
using COINSMobile.stockwebservice;

namespace COINSMobile
{
    [Activity(Label = "Market Watch", Icon = "@drawable/icon_activity")]
    public class MarketWatchActivity : Activity
    {
        private bool IsLoading = false;
        private TableLayout tablelayout = null;

        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.MarketWatch);

            tablelayout = FindViewById<TableLayout>(Resource.Id.deatlWatchLayout);

            ProgressDialog progress = ProgressDialog.Show(this, "", "Loading Market Watch...", true);
            new Thread(new ThreadStart(() =>
            {
                this.RunOnUiThread(() =>
                {
                    doLoadMarketWatch();
                    progress.Dismiss();
                });
            })).Start();
            IsLoading = false;

            ImageView btnRefresh = FindViewById<ImageView>(Resource.Id.watchRefresh);
            btnRefresh.Click += (sender, e) =>
            {
                if (!IsLoading)
                {
                    ProgressDialog progress1 = ProgressDialog.Show(this, "", "Loading Quote...", true);
                    new Thread(new ThreadStart(() =>
                    {
                        this.RunOnUiThread(() =>
                        {
                            doLoadMarketWatch();
                            progress1.Dismiss();
                        });
                    })).Start();
                    IsLoading = false;
                }
            };

            
        }
        private bool doLoadMarketWatch()
        {
            bool ret = false;
            string strStocks = string.Empty; //create a comma delimited stock name from database

            IEnumerable<Stock> stockdata = StockManager.GetStocks();
            foreach (Stock rec in stockdata)
            {
                strStocks += rec.StockName + ",";
            }
            if (strStocks.Length > 0) strStocks.TrimEnd(',');
            if (strStocks != string.Empty)
            {
                PopulateDataToControls(strStocks);
            }
            else
            {
                //clear all views from table layout
                tablelayout.RemoveAllViews();
                tablelayout.RefreshDrawableState();
            }
            return ret;
        }

        private void PopulateDataToControls(string _stocks)
        {
            //we have now the stocks delimited by comma
            //this string will be passed to Webservice as a paramete to fetch the stck block in xml

            string strXML = string.Empty;
            stockwebservice.StockWebservice quoteObject = null;
            try
            {
                quoteObject = new stockwebservice.StockWebservice();
                strXML = quoteObject.GetStockQuote(_stocks);
            }
            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;
            }
            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;
            }

            //load the xml to XmlDocument
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(strXML);

            tablelayout.RemoveAllViews();
            tablelayout.RefreshDrawableState();

            XmlNodeList xnList = doc.SelectNodes("/stock/symbol");
            foreach (XmlNode xn in xnList)
            {
                if (xn != null)
                {
                    TableRow demoTableRow = new TableRow(this);
                    TextView tv_l = new TextView(this);
                    TextView tv_r = new TextView(this);
                    
                    tv_l.SetPadding(3, 3, 3, 3);
                    tv_r.SetPadding(3, 3, 3, 3);
                    tv_r.Gravity = GravityFlags.Right;
                    tv_l.SetTextSize(Android.Util.ComplexUnitType.Px, 9);


                    tv_l.Text = xn["code"].InnerText.Trim() + "-" + xn["exchange"].InnerText.Trim();
                    tv_r.Text = "(" + xn["change"].InnerText.Trim() + ") " + xn["last"].InnerText.Trim();

                    demoTableRow.Clickable = true;
                    demoTableRow.Click += (sender, e) =>
                    {
                        doRowClick(sender);
                    };

                    demoTableRow.AddView(tv_l);
                    demoTableRow.AddView(tv_r);

                    tablelayout.AddView(demoTableRow);
                    View vLineRow = new View(this);
                    vLineRow.SetMinimumHeight(1);
                    vLineRow.SetBackgroundColor(Color.Rgb(88, 88, 88));
                    tablelayout.AddView(vLineRow);
                }
            }
        }

        private void doRowClick(object Sender)
        {
            TableRow tr = Sender as TableRow;
            if (tr != null)
            {
                TextView v = tr.GetChildAt(0) as TextView;
                string _script = v.Text.Trim();
                int _index = _script.IndexOf('-');
                if (_index > 0) _script = _script.Substring(0, _index);

                if (_script != string.Empty)
                {
                    var t = Toast.MakeText(this, _script, ToastLength.Short);
                    t.SetGravity(GravityFlags.Center, 0, 0);
                    t.Show();


                    var intent = new Intent();
                    intent.SetClass(this, typeof(StockDetailsActivity));
                    intent.PutExtra("Script", _script);
                    StartActivity(intent);
                }
            }
        }


        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            menu.Add("Get Quote").SetIcon(Resource.Drawable.ic_stock_get); ;
            menu.Add("Add Stock").SetIcon(Resource.Drawable.ic_quote_add);
            menu.Add("Delete Stocks").SetIcon(Resource.Drawable.ic_stock_delete);
            return true;
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.TitleFormatted.ToString())
            {
                case "Delete Stocks":
                    doDeleteStocks(); break;
                case "Add Stock":
                    doOpenAddStock(); break;
                case "Get Quote":
                    doOpenGetStock(); break;
            }
            return base.OnOptionsItemSelected(item);
        }

        protected void doDeleteStocks()
        {
            StockManager.DeleteAllStocks();
            if (!IsLoading)
            {
                ProgressDialog progress1 = ProgressDialog.Show(this, "", "Deleting Stock...", true);
                new Thread(new ThreadStart(() =>
                {
                    this.RunOnUiThread(() =>
                    {
                        doLoadMarketWatch();
                        progress1.Dismiss();
                    });
                })).Start();
                IsLoading = false;
            };
        }
        protected void doOpenAddStock()
        {
            StartActivity(typeof(AddStockActivity));
        }
        protected void doOpenGetStock()
        {
            StartActivity(typeof(GetStockActivity));
        }
    }
}

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)

Share

About the Author

Sandip.Nascar
Architect
India India
Sandip is a "Software Architect and .NET Consultant".

He has 20 years of 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, Silverlight, 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
View My Page

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 24 Mar 2013
Article Copyright 2012 by Sandip.Nascar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid