Skip to content

Instantly share code, notes, and snippets.

@clamytoe
Last active August 29, 2015 14:27
Show Gist options
  • Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
HKUSTx: COMP107x Introduction to Mobile Application Development using Android
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise: Greet Friend (with ListActivity)\n",
"---\n",
"## Objectives\n",
"\n",
"In this exercise we see the use of ListActivity and constructing an UI with a list of items. At the end of this exercise you should be able to:\n",
"\n",
"* Use a ListActivity to construct an UI with a list of items\n",
"* Use a layout to specify the layout for each item in the list\n",
"* Use the string-array resource\n",
"\n",
"\n",
"## Introduction\n",
"\n",
"We continue with the modification of the GreetFriend application. In this exercise, we make use of a ListActivity to display a list of friends and allow the user to select one of the friends from the list. The app will then display the greeting message using the ShowMessage activity as before.\n",
"\n",
"## Textual Instructions\n",
"---\n",
"### Step 1: Adding String Resources\n",
"\n",
"1. Open the **strings.xml** file and update its contents by adding the string-array resource as shown below.\n",
"\n",
" <resources>\n",
" <string name=\"app_name\">GreetFriend</string>\n",
"\n",
" <string name=\"hello_world\">Hello world!</string>\n",
" <string name=\"action_settings\">Settings</string>\n",
" <string name=\"greetstring\">\"Good Day \"</string>\n",
" <string name=\"title_activity_show_message\">ShowMessage</string>\n",
"\n",
" <string-array name=\"friends\">\n",
" <item>John</item>\n",
" <item>Paul</item>\n",
" <item>George</item>\n",
" <item>Ringo</item>\n",
" </string-array>\n",
"\n",
" </resources>\n",
"\n",
"### Step 2: Adding a Layout for the ListItem\n",
"\n",
"1. Right click on **res->layout** folder and add a new resource file. Specify the name of the file as *friend_item* and the root element as *TextView* in the selection window.\n",
"\n",
"2. Once the layout file named **friend_item.xml** is created, edit the properties of the **TextView** by setting its ***text*** to \"*Friend Name*\", its ***gravity*** to *center_horizontal* and *center_vertical*, its ***padding*** to *20dp* and ***text size*** to *24sp*.\n",
"\n",
"---\n",
"### Step 3: Modifying the Code\n",
"\n",
"1. Open **MainActivity.java** file and update the class definition and the onCreate() method as shown below.\n",
"\n",
" public class MainActivity extends ListActivity {\n",
"\n",
"\n",
" String [] names;\n",
"\n",
" @Override\n",
" protected void onCreate(Bundle savedInstanceState) {\n",
" super.onCreate(savedInstanceState);\n",
"\n",
" // Construct a string array from the String Array resource \"friends\" in the\n",
" // strings.xml file\n",
" names = getResources().getStringArray(R.array.friends);\n",
"\n",
" // This method call sets the names string array as the source of the names\n",
" // for the list of items in the listview. The ArrayAdapter object is used\n",
" // to adapt the string array and construct a list of layout items that are\n",
" // then used by the ListView of the ListActivity to construct the list of friends.\n",
" // The layout of each item is specified by the friend_item.xml file\n",
" setListAdapter( new ArrayAdapter<String>(this, R.layout.friend_item, names));\n",
"\n",
" }\n",
"\n",
"2. Then, at the bottom, replace the onClick() method with the following onListItemClick() method code as shown below.\n",
"\n",
" @Override\n",
" protected void onListItemClick(ListView l, View v, int position, long id) {\n",
" super.onListItemClick(l, v, position, id);\n",
"\n",
" // The parameter \"id\" indicates the index of the item that was selected from\n",
" // the list of friends. This is used to index into the names[] array to get\n",
" // the name of the friend selected. Rest of the code is similar to the earlier\n",
" // exercise.\n",
"\n",
" // create a new intent. The first parameter is the Context which is the current Activity.\n",
" // Hence we use \"this\". The second parameter is the Activity class that we wish to start.\n",
" // Hence it is specified as ShowMessage.class\n",
" Intent in = new Intent(this,ShowMessage.class);\n",
"\n",
" // Add the message as a payload to the Intent. We add data to be carried by the intern using\n",
" // the putExtra() methods. The data is specified as a key-value pair. The first parameter is\n",
" // the key, specified as a string, and the second parameter is the value.\n",
" in.putExtra(\"message\", getString(R.string.greetstring) + names[(int) id] + \"!\");\n",
"\n",
" // We start the new activity by calling this method to inform the Android framework to start\n",
" // the new activity. The parameter is the Intent we just created earlier\n",
" startActivity(in);\n",
"\n",
" }\n",
"\n",
"3. Run the application and see the result."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment