Welcome to part 6 of my Android Development Tutorial! Over the next few videos I will cover a tremendous amount of information. You can get the whole app right now if you can’t wait here Android Stock Quote App.
Here are just a few of the things I’ll cover over the next few videos: The TableLayout : Dynamically Fill ScrollViews : Save Key / Value Pairs : Review of ClickListeners : Using Intents to Open other Applications : Alert Dialog Boxes : Start other Activities and Pass in Information : Yahoo Web Service : YQL : AsyncTask : Threads : Parsing XML
If you like videos like this, it helps to tell Google+ with a click here [googleplusone]
Code From the Video
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Get Stock Quotes</string> <string name="action_settings">Settings</string> <string name="stock_symbol">Enter Symbol</string> <string name="enter_stock_symbol">Enter</string> <string name="stock_symbol_list">Stock Symbols and Quotes</string> <string name="change_stock_symbol">Change</string> <string name="ok">OK</string> <string name="cancel">Cancel</string> <string name="missing_stock_symbol">Enter a Stock Symbol</string> <string name="invalid_stock_symbol">Invalid Stock Symbol</string> <string name="delete_all_symbols">Delete All Symbols</string> <string name="confirm_action">Are You Sure?</string> <string name="get_stock_quote">Quote</string> <string name="go_to_website">Web</string> <string name="yahoo_stock_url">http://finance.yahoo.com/q?s=</string> <string name="stock_company_name">Company Name</string> <string name="stock_year_low">Year Low:</string> <string name="stock_year_high">Year High:</string> <string name="stock_days_low">Days Low:</string> <string name="stock_days_high">Days High:</string> <string name="title_activity_stock_info">StockInfoActivity</string> <string name="last_trade_price_only">Last Price:</string> <string name="change">Change:</string> <string name="days_range">Daily Price Range:</string> </resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- One way to define custom colors --> <color name="aero_blue">#7CB9E8</color> <color name="egg_shell">#F0EAD6</color> <color name="black">#000000</color> </resources>
dimens.xml
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">5dp</dimen> <dimen name="activity_vertical_margin">5dp</dimen> <dimen name="stock_list_text_size">16sp</dimen> </resources>
activity_main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/TableLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/enterSymbolTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stock_symbol" /> <EditText android:id="@+id/stockSymbolEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="text" android:layout_weight="1"> <requestFocus /> </EditText> <Button android:id="@+id/enterStockSymbolButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/enter_stock_symbol" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/egg_shell" > <TextView android:id="@+id/stockSymbolTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_span="2" android:padding="5dp" android:textColor="@color/black" android:gravity="center_horizontal" android:text="@string/stock_symbol_list" android:textSize="@dimen/stock_list_text_size" /> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/egg_shell" android:layout_weight="1" > <ScrollView android:id="@+id/stockScrollView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_span="2" android:padding="5dp" > <TableLayout android:id="@+id/stockTableScrollView" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:stretchColumns="yes" > </TableLayout> </ScrollView> </TableRow> <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/deleteStocksButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/delete_all_symbols" android:layout_span="2" android:layout_weight="1" /> </TableRow> </TableLayout>
stock_quote_row.xml
<?xml version="1.0" encoding="utf-8"?> <TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/stockSymbolTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" android:padding="@dimen/activity_horizontal_margin" > <TextView android:id="@+id/stockSymbolTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_span="2" android:layout_weight="1" android:textColor="@color/black" android:textIsSelectable="true" /> <Button android:id="@+id/stockQuoteButton" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/get_stock_quote" /> <Button android:id="@+id/quoteFromWebButton" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/go_to_website" /> </TableRow>
I copied these files over so i can listen more while you explain, and none of my things are style like yours is in the video. None of the things are centered, it would be a great help if you could help me out with this. Thank you for the great tutorials
actually its only when i create it in the same workspace as the other apps we have made previously.
Did you get it to work then? Sorry I couldn’t answer quicker. The app should look exactly the same
You should try to watch the video and actually do everything yourself following Derek.
I’ve done so and the layout of my app and that of the app described in the tutorial match perfectly (normally, the workspace folder that is set shouldn’t matter).
Take the time to write the code yourself instead of just copying everything – I’m sure you know these two are very different things in terms of learning.
There is android:gravity=”center” and android:layout_gravity=”center”. You sometimes have to set them both to get the center effect.
android:layout_gravity=”center_vertical android:gravity=”center_vertical”
duplicate the tableRow1 in activity_main.xml at string 013 and 087
duplicate the tableRow1 in activity_main.xml at line 013 and 087.
and TableLayout in activity_main.xml at line 076 need android:id=”@+id/stockTableScrollView”
Hello Derek,
there is a small bug on stock_quote_row.xml
the right code is
android:id=”@+id/stockSymbolTextView”
Sorry about that. The code in the package I provide should be ok though. Thank you for pointing that out
Hey Derek, no one does detailed tutorials like you. This is awesome. Thanks a ton man
Thank you 🙂 I do my best to constantly improve. I’m very happy that you like them
Seems that there are problems here
081
You don’t have here id but in the next tutorial you do something like
// Table inside the scroll view that holds stock symbols
// and buttons
private TableLayout stockTableScrollView;
// in onCreate
stockTableScrollView = (TableLayout) findViewById(R.id.stockTableScrollView);
Look at the java package I provide with the finished code. It has just as many comments, but it runs without any issues
Hi Derek
Let me first complement and thank you for the superb job you are doing. Now about the run time error “android.widget.ScrollView cannot be cast to android.widget.TableLayout”, which many people encounter on your StockQuotes tutorial. Sophie had the same problem but She did not explain the problem and the solution well enough. The issue is that on your tutorial #6 You have :
That is to say you assign an id to “ScrollView” but you don’t assign an id to “TableLayout”. It turns out, it should be the other way around. You need to assign the id to the “TableLayout” and not to “ScrollView”. So the solution for those who get the above mentioned run time error is that they must remove the id from “ScrollView” and assign it to “TableLayout” as follows:
That should take care of the problem.
Mike
For some reason the code did not show up in my previous comment. About: Run-time error “android.widget.ScrollView cannot be cast to android.widget.TableLayout”.
The wrong way: Pay attention to id lines.
ScrollView
android:id=”@+id/stockTableScrollView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_span=”2″
android:padding=”5dp”
TableLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:padding=”5dp”
android:stretchColumns=”yes”
TableLayout
The right way: Pay attention to id lines.
ScrollView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_span=”2″
android:padding=”5dp”
TableLayout
android:id=”@+id/stockTableScrollView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:padding=”5dp”
android:stretchColumns=”yes”
TableLayout
Mike
Mike thanks for the tip ran into the same thing.
This is a realy awesome tuterial by the way!!!
Hi Derek
Let me first complement and thank you for the superb job you are doing. Now about the run-time error “android.widget.ScrollView cannot be cast to android.widget.TableLayout” which many people encounter on your StockQuotes tutorial #6 and beyound. Sophie had the same problem but She did not explain the problem and the solution well enough. The issue is that on your tutorial #6 You have :
The wrong way:
ScrollView
android:id=”@+id/stockTableScrollView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_span=”2″
android:padding=”5dp”
TableLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:padding=”5dp”
android:stretchColumns=”yes”
TableLayout
ScrollView
That is to say you assign an id to “ScrollView” but you don’t assign an id to “TableLayout”. It turns out, it should be the other way around. You need to assign the id to the “TableLayout” and not to “ScrollView”. So the solution for those who get the above mentioned run-time error is that they must remove the id from “ScrollView” and assign it to “TableLayout” as follows:
The wrong way. Pay attention to id lines
ScrollView
android:id=”@+id/stockTableScrollView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_span=”2″
android:padding=”5dp”
TableLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:padding=”5dp”
android:stretchColumns=”yes”
TableLayout
ScrollView
The correct way. Pay attention to id lines
ScrollView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_span=”2″
android:padding=”5dp”
TableLayout
android:id=”@+id/stockTableScrollView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:padding=”5dp”
android:stretchColumns=”yes”
TableLayout
ScrollView
Mike
Hi Mike,
Ill have to look into that. Thank you for the input 🙂
Some people seem to have the same problem because they are entering a comma in the price amount. I guess that is common in other countries?
I didn’t have time to cover everything and provide 100% error checking. Sorry about that.
thanQ for your great support
You’re very welcome 🙂
Yes, all of Europe, South America and Russia uses the komma as decimal seperator.
http://www.statisticalconsultants.co.nz/blog/how-the-world-separates-its-decimals.html
It gave problems with my program too in previous tipApp. Solution was to change “region and language” setting in my windows installation to English.
Sorry about that. I wrote the code out of my head and didn’t know about the comma
Thanks Derek
You’re welcome 🙂
Hi, How I can read an XML content but only if I have been identified by a username and password?
I added the requests to the list. Thank you 🙂
Not sure if you mention the manifest file but, the quote button wouldn’t work until I set the permission for the device to use the internet in the android manifest xml.
Sorry about that. I should have made that clearer. Thanks for pointing that out
can u pls show me the what is the use of jquerymobile and jqueryui and how to use them in our project or app…
I’m going to cover HTML5 solutions for cross platform app development soon. Thank you for the request 🙂
Hi Derek ,
thank you for these great tutorials, I make my debut in programming for android and they are very helpful to me . However, I currently have a problem with the interface. When I desire to center the text in the textView , I use the command android : gravity = ” center_horizontal ” but when I look at the interface, the text is shifted to the right. I feel I should set limits . If I understand correctly, the android command: layout_width = ” match_parent ” defined limits .
ex:
< TextView
android: id = "@ + id / stockSymbolTextView "
android: layout_width = " match_parent "
android: layout_height = " wrap_content "
android: layout_span = "2"
android: gravity = " center_horizontal "
Currently I use Android Developer Tools Build : 887826 v22.3.0 – one Pc and win7
All updates have been made.
Are you already seen this?
Thank you . I hope you can help me.
Alex .
Hi Alex,
Thank you for the kind compliment 🙂 Relative layouts can some times not work perfectly. Have you tried android:gravity=”center”
You may also want to try messing with weight if you are still having problems.
Tell me if that doesn’t work
Derek
Hi Derek,
Love the tutorials. Thanks for spending the time to teach us newbies! I have watched tutorials 6 – 8 for the multiple activity stock quote picker and I still can’t get it working.
Do I need to edit the android manifest aswell? I couldn’t find it in the code you posted. The application just seems to crash as soon as I run it. Also, do I need to use a specific build? I’m using 4.0.3.
Thanks for your help!
Dan
Hi Dan,
Sorry, but I think Yahoo changed their data. You’ll have to look at it and use the different tags they now use. I’ll be making an updated web services tutorial in the next week. That will allow everything to always work and also teach how to make your own web services.