Android Development Tutorial 6

Android Development TutorialWelcome 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

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: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>

29 Responses to “Android Development Tutorial 6”

  1. Andrew says:

    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

    • Andrew says:

      actually its only when i create it in the same workspace as the other apps we have made previously.

      • Derek Banas says:

        Did you get it to work then? Sorry I couldn’t answer quicker. The app should look exactly the same

      • Victor says:

        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.

  2. Vadim says:

    duplicate the tableRow1 in activity_main.xml at string 013 and 087

    • Vadim says:

      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”

  3. Kyle says:

    Hey Derek, no one does detailed tutorials like you. This is awesome. Thanks a ton man

  4. Pawel says:

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

  5. Mike M. says:

    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

  6. Anonymous says:

    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

  7. Mike M. says:

    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

  8. Anonymous says:

    Thanks Derek

  9. Pepo says:

    Hi, How I can read an XML content but only if I have been identified by a username and password?

  10. Lyca says:

    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.

  11. numa says:

    can u pls show me the what is the use of jquerymobile and jqueryui and how to use them in our project or app…

  12. Alexandre says:

    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 .

    • Derek Banas says:

      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

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Google+