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
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise: Greet Friend (Part 2)\n",
"---\n",
"## Objectives\n",
"\n",
"At the completion of this exercise, you will be able to:\n",
"\n",
"* Activate UI widgets and buttons\n",
"* Implement event listeners to listen to UI events like button clicks\n",
"\n",
"## Introduction\n",
"\n",
"In the previous exercise, we constructed the UI for our Greet Friend exercise and we were able to deploy the app. However, nothing happens when the button is clicked. This is because we did not activate the widgets in the code.\n",
"\n",
"## Textual Instructions\n",
"---\n",
"### Referencing the UI widgets in Java Code\n",
"\n",
"We will now learn how to reference any UI widget from within the Java code. This will enable us to access, respond to user interactions and update the state of the widgets within the Java code.\n",
"\n",
"1. Open **MainActivity.java** file. Let us now get a reference to the **greetButton**. Modify the Java code as shown below.\n",
" \n",
" public class MainActivity extends Activity {\n",
"\n",
" Button greetButton;\n",
"\n",
" @Override\n",
" protected void onCreate(Bundle savedInstanceState) {\n",
" super.onCreate(savedInstanceState);\n",
" setContentView(R.layout.activity_main);\n",
"\n",
" greetButton = (Button) findViewById(R.id.greetButton);\n",
" greetButton.setOnClickListener(this);\n",
" }\n",
"\n",
"The button **greetButton** variable is declared on line 4. Subsequently, the reference to the **greetButton** is obtained on line 11. Note the use of the id of the **greetButton** in the findViewById() method. Furthermore, on line 12 the OnClickListener for the **greetButton** is set to be this class, thus expecting the MainActivity class to implement the OnClickListener for the button.\n",
"\n",
"---\n",
"### Step 2: Activating the Button using OnClickListener\n",
"\n",
"The implementation of the onClickListener for the button can be accomplished as follows:\n",
"\n",
"1. Modify the class declaration line to indicate that the class implements the OnClickListener as follows.\n",
"\n",
" public class MainActivity extends Activity implements View.OnClickListener {\n",
" \n",
"2. Then, implement the OnClickListener as follows. Pay particular attention to the comments in the code to understand what is being implemented.\n",
"\n",
" @Override\n",
" public void onClick(View v) {\n",
"\n",
" // get a reference to the TextView on the UI\n",
" TextView textMessage = (TextView) findViewById(R.id.textMessage);\n",
"\n",
" //get a reference to the EditText so that we can read in the value typed\n",
" // by the user\n",
" EditText editFriendName = (EditText) findViewById(R.id.editFriendName);\n",
"\n",
" // get the name of the friend typed in by the user in the EditText field\n",
" String friendName = editFriendName.getText().toString();\n",
"\n",
" switch (v.getId()) {\n",
"\n",
" case R.id.greetButton:\n",
"\n",
" // set the string being displayed by the TextView to the greeting\n",
" // message for the friend\n",
" textMessage.setText(\"Good Day \"+friendName+\"!\");\n",
"\n",
" break;\n",
"\n",
" default:\n",
"\n",
" break;\n",
" }\n",
" }\n",
" \n",
"3. Now if you run the example, you will note that the app works as expected."
]
}
],
"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.
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