2017년 1월 23일 월요일

Paint Pot Extended with Camera (AI2)


✼✼contents✼✼

Scan the QR code with your Android device to download the application.




Step 1: Introduction

Step 2: Build the user interface

Step 3: Draw Line and Circle

Step 4: Add code so that the user can draw in different colors.

Step 5: Add code so the user can draw circles of different sizes..

Step 6: Add Camera

Step 7: Add Slider


StockQuotes for App Inventor 2


StockQuotes

What You're Building

Just like you can access web pages from your phone -- for example, to look up a stock price -- so can App Inventor. This app enables the user to enter a stock symbol, then looks up the price of the stock on Yahoo! Finance and displays the price on the phone.
This tutorial assumes that you are familiar with the basics of App Inventor -- using the Component Designer to build a user interface and using the Blocks Editor to specify event handlers. If you are not familiar with the basics, try stepping through some of the basic tutorials before continuing.

Introduction

This tutorial includes
1. Accepting text input from the user to specify the stock symbol.
2. Using the Web component to ask Google Finance for the latest price for the stock.
3. Displaying the result.

Getting Started

Connect to the App Inventor web site and start a new project. Name it StockQuotes, and also set the screen’s Title to “Stock Quotes”. Open the Blocks Editor and connect it to the phone.

Set up the Components

Use the component designer to create the user interface. When you are done, it should look something like the picture below. Additional instructions are below the picture.
Create the following components by dragging them from the Palette into the Viewer.
Component TypePalette GroupWhat you'll name itPurpose of Component
TextBoxUser InterfaceStockSymbolTextBoxWhere the user enters the stock symbol
ButtonUser InterfaceGetQuoteButtonTo request the stock quote
LabelUser InterfaceValueLabelTo display the stock quote
WebConnectivityWeb1To request and receive the stock quote
Stick with the default properties except for the following changes:
ComponentAction
StockSymbolTextBoxSet its Hint property to "Enter a stock symbol".
GetQuoteButtonSet its Text property to "Get Stock Quote".
ValueLabelClear its Text property.

The Yahoo! Finance API

Many web services provide an application programmer interface (API) for developers to enable their programs to access the service. Some ways to discover APIs are through the website http://programmableweb.com or just by doing a web search for the service name and “API”.
For example, you can use the Yahoo! Finance API to get the latest price for the stock with the symbol "GOOG", for example, with the URL http://finance.yahoo.com/d/quotes.csv?f=l1&s=GOOG . The section "f=l1" (lower-case letter L, followed by the number 1) says we would like the latest price, and the section “s=GOOG” says we would like information about the stock whose symbol is “GOOG”. The result is returned in comma-separated value (CSV) format, which you may be familiar with from spreadsheets. Since only one value will be returned for our query, the result will be a plain old number, such as “511.5”, without any commas. (Commas would be used if we requested multiple pieces of data from Yahoo!, such as the name of the company and the daily trade volume.)

Add Behaviors to the Components

Requesting the Data
The blocks to make the web request are shown here and detailed below:
Block typeDrawerPurpose
GetQuoteButton.ClickGetQuoteButtonHandle a click of the "Get Quote" button.
set Web1.Url toWeb1Specify the URL to request.
joinTextConcatenate the parts of the URL.
" "
(http://finance.yahoo.com/d/quotes.csv?f=l1&s=)
TextSpecify the first unchanging part of the URL.
StockSymbolTextBox.TextStockSymbolTextBoxGet the stock symbol from the text box.
call Web1.GetWeb1Make the web request.
The meaning is: When GetQuoteButton is clicked:
1. Build the Web component’s URL by concatenating “http://finance.yahoo.com/d/quotes.csv?f=l1&s=” (which you should copy and paste into the text block) and the symbol entered by the user (StockSymbolTextBox.Text).
2. Request the page specified by the URL. (This is like pressing return after entering a URL in your web browser.)
Receiving the Data
When the response to the web request arrives, the Web.GotText event is raised with four parameters (only some of which we’ll use in this app):
1. url: the URL of the original request (which is useful if requests are made with many different URLs).
2. responseCode: the HTTP status code, which indicates whether the web request succeeded or how it failed; for example, 200 means that the request succeeded, 404 that the page could not be found, etc.
3. responseType: the MIME type of the response, such as “text/csv” in this app, “image/jpeg”, etc.
4. responseContent: the data being returned, such as “511.5”.
Here are a picture and table of the blocks you need to create:
Block typeDrawerPurpose
Web1.GotTextWeb1Specify what to do when the reply comes back from the web.
if thenControlProvide different behavior depending on whether the request succeeded. Use the mutator to add an else statement.
get responseCodeVariablesGets the response code returned for the web request, which...

(equals) block
Logic...is checked for equality with...
number (200)Math...200, the code for valid web responses.
set ValueLabel.Text toValueLabelDisplay the result on the screen.
joinTextBuild the result by concatenating...
" "
("Current value: ")
Text...the text “Current value: “ and...
get responseContentVariablesGets the value returned from the web.
set ValueLabel.Text toValueLabelDisplay an error message.
" "
("Error getting stock quote")
TextThe error message
Here's a description of the block's behavior:
1. If the response code indicates that the web request succeeded (= 200), set the label to the concatenation of “Current value: “ and the returned data (e.g., 511.5).
2. Otherwise, set the label to “Error getting stock quote”.

Review

Here are some ideas introduced in this tutorial:
1. Using an application programmer interface (API)
2. Making a request with the Web component
3. Checking whether a web request was successful
4. Displaying information returned from the web
These ideas will be developed further in the second part of this tutorial, which is under development.

Scan the Sample App to your Phone

Scan the following barcode onto your phone to install and run the sample app.

Download Source Code

If you'd like to work with this sample in App Inventor, download the source code to your computer, then open App Inventor, click Projects, choose Import project (.aia) from my computer..., and select the source code you just downloaded.

Credits

This tutorial is based on an app created by Prof. David Wolber and relies on the Yahoo! Finance API. Done with StockQuotes? Return to the other tutorials here.
Tutorial Version: App Inventor 2
Tutorial Difficulty: Intermediate
Tutorial Type: External API


Teaching with App Inventor


App Inventor Concept Cards and Maker Cards

Concept cards offer concise summaries of some of the basic concepts of making apps. Great for use in the classroom or on your own. Maker Cards help new users get started quickly with mini-apps that can be made during the course of a short workshop or class.

Teaching App Creation with App Inventor

Teaching app creation is a 6-week online course intended for teachers instructing teams of students involved in the Verizon Innovative App Challenge or anyone interested in teaching with App Inventor. The course combines software design processes, programming with App Inventor, and pedagogical approaches to give teachers a complete introduction to both computer science principles and App Inventor itself.

Running a workshop or teaching a first class?

These short, introductory apps get your students to that "a-ha" moment quickly and easily, leaving them ready to dig deeper into the powerful capabilities of App Inventor. These are the videos that the App Inventor team uses for introductory workshops. Students can build three apps: TalkToMe, BallBounce, and DigitalDoodle by watching the vidoes or by following the step-by-step pictures and text on the PDF versions of the tutorials, which are linked at the bottom of the page. Happy Inventing!

AppInventor.org

Offering a Course-in-a-Box, the App Inventor Book (free online!), video tutorials, a concept map, and more, AppInventor.org is a site you'll want to bookmark. Led by David Wolber at University of San Francisco, this team offers resources to help you run any course whether it's for a day or a semester.

Mobile Computer Science Principles

Mobile Computer Science Principles (Mobile CSP) is based on the College Board's emerging Advanced Placement (AP) course in computer science. Students learn computer science by building socially useful mobile apps. This course was developed by Ralph Morelli and his team at Trinity College.

Links to App Inventor Curricula

Here you'll find links to materials developed by the Mobile Computational Thinking Project, as well as other great curricula and resources developed by educators from around the world.

We need you to contribute to App Inventor's Resources!

We'd love to share your tutorials or other resources with the App Inventor community. Send us a hyperlink or a document: aiwebreview@mit.edu (link sends e-mail)

Other Sources of Teaching Materials:

Magic 8-ball for App Inventor 2


Lesson One: Magic 8-Ball Predicts the Future

This introductory module will guide you through building a “Magic 8-Ball” app with App Inventor 2. When activated, your 8-ball will deliver one of its classic predictions, such as “It is decidedly so” or “Reply hazy, try again.”

Learning Goals

After completing this app, you will be able to:
⦁ Navigate the App Inventor environment: designer, blocks editor, emulator and/or physical phone
⦁ Correctly use the following App Inventor components: accelerometer sensor, button, player
⦁ Correctly use the following App Inventor concepts: making and using a list, responding to an event

Materials

⦁ A selection of images and sounds are available at the App Inventor Media Library.

Outline

1. Set up computers and phones or emulators. (Suggestion: do this ahead of time)
5. Suggestions for further exploration: Text-to-Speech, Rotating image, Custom prediction lists

Part One: Click a Button, Hear a Sound

The final Magic 8-Ball App will deliver a prediction from a list that you have designed. To get started, first we'll make a button with a picture on it, and program it to play a sound when the button is clicked.
DESIGN: App Inventor Designer
1. To open the App Inventor Designer window, open a new tab or window in your browser and go to http://newblocks.appinventor.mit.edu. See setup instructions if you are not sure how to sign in.
2. If you have already made an app (such as Hello Purr), you will automatically be directed to the Designer with the last project you worked on showing. Click "Project" in the upper left corner of the screen and then "My Projects", which will take you to your list of projects. Click "New Project" and name your project something like "Magic8Ball" (note: spaces are not allowed).
3. Download one image and one sound file from below to be used in your app. Right click (control-click) on the link of the image or sound, then choose "Download" or "Save As". Save the media files to a convenient location that you will remember.
4. On the left column of the Designer, the User Interface palette should be open. If it is not, click to open it. Drag a Button component over to the Viewer (#1).
5. Set the button image to an 8-Ball image:
Click on your newly added button to see its properties in the Properties pane on the right. Under "Image" click on the word "None..." and a small selection window will pop up (#2). Click the "Upload File" button and browse to where you saved the 8-Ball image. Select the file, then click “OK” to close the selection window. Click “OK” again on the properties pane to close the small popup window (#3).
6. Go to the text field in the Properties pane and delete the display text of your button component (#4).
7. From the Media palette, drag over a Player component onto the Viewer pane (#1). The Player component plays sound files. Notice that since the player will not be a visible part of the app, it appears at the bottom of the Viewer pane, as a “Non-visible component”.
8. Set the Player component's source file:
Click on your newly added Player component to see its properties in the Properties pane on the right. Under "Source" click in the small box on the word "None..." and a small selection window will pop up (#2). Click the "Upload File" button and browse to where you saved the sound file. Select the sound file, then click “OK” to close the selection window. Click “OK” again on the properties pane to close the small popup window (#3).
    9. You have now completed the work in the Designer for Part One of this app. It's time now to go over to the Blocks Editor to program the behavior of these components.
BUILD: Blocks Editor
In the upper right corner of the Designer, click on the Blocks button.
Now you are going to tell your app how to behave when the button is clicked. This is actually very simple in App Inventor, because the "code" for the program only consists of two blocks!
Once the Blocks Editor is open, there are several options running along the left side of the screen. We refer to these as "Palettes" with “Drawers.”
From the blocks palette located under Screen1, click on the Button1 drawer. Drag the when Button1.Click block into the work area (#1). From the blocks palette, click on the Player1 drawer, drag the Player1.Play block into the work area and insert it into the when Button1.Click block (#2). They will click together like magnetic puzzle pieces.
Your blocks should now look like this:
That's it! You've written the program for Part One of Magic 8-Ball. Now it's time to test that it's working right.
TEST: Phone/Emulator
You have now built an app! To test that it works, you either have to launch an emulator, or connect to a phone. Go back to the Setup Instructions if you do not have a phone or an emulator running.
Emulator: click on the picture, you will hear the sound play.
Phone: tap the picture, you will hear the sound play.
Note: If you don't hear the sound, first be sure you have the volume turned up on your device (or computer if using emulator). Also, make sure your device has an SD card. App Inventor stores media files to the SD card.

Part Two: Click the Button, Get a Prediction + Hear a Sound

Now that we've gotten the button to perform an action (play a sound), we want to extend that action to include giving the user a prediction. First we'll need two labels: Label1 will display the instructions, and Label2 will display the chosen prediction. We'll use blocks to program a "list picker" to choose from a list of predictions. Each time the button is clicked, the app will change the text of Label2 to display the chosen prediction.
DESIGN: App Inventor
Go back to the Designer window in your browser and add some new things to your app.
1. From the Screen Arrangement palette, drag over the Vertical Arrangement component (#1). At first it will just look like an empty box, but when you put things in it, App Inventor will know that you want to line them up vertically (one on top of the other).
    2. From the Basic palette, drag over a Label component (#2) and drop it inside of the vertical arrangement component. In the Properties pane, change the "Text" property of Label1 to “Ask the Magic 8-Ball a question”.(#3)
    3. From the Basic palette, drag over another Label component (Label2) into the Vertical Arrangement box so that it sits right below Label1. Change the "Text" property of the Label2 to “Touch the Magic 8-Ball to receive your answer.” Now drag the 8-Ball image so that it is also inside the Vertical Arrangement component on top of the two labels. This will cause them to line up with each other in a vertical line. (Note: this can be tricky mouse work, but get them in there just right and the vertical arrangement will resize itself to fit everything.)
Now it’s time to go back into the Blocks Editor to program the components you just added to your project.
BUILD: Blocks Editor
Now for the fun part! You're going to make a list of predictions and program the button to pick one item from the list and display it inside Label2. The button will also still play the sound that you programmed in Part One. Here's how to do it...
1. From the blocks palette, click on Label2 drawer to see all of its associated blocks. Drag over the green set Label2.BackgroundColor and insert it just above the Player1.Start block. Notice that the when Button1.Click block automatically gets bigger to accomodate the new block.
2. Clicking on the word "BackgroundColor" will allow you to change the property that is being set. We want to change the Text so our block will look like set Label2.Text.
3. From the Built-In palette, click on the Lists drawer. Drag over the pick random item block and connect it to the open socket of the set Label2.Text block.
4. From the Built-In palette, click on Lists again, then drag out the make a list block and plug it into the "list" socket on the right side of the pick random itemblock.
5. From the Built-In palette, click on the Text drawer, drag out a " " block and connect it to the item socket of the make a list block. Click directly on the space in the block. You can then type in text there. Think about the sayings you want in your list of predictions for the Magic 8-Ball. Type the first prediction into this new text block.
6. Notice after you plug in two text blocks, there are no more sockets to add more responses. To create more sockets, you need to click the dark blue button on the block. Make a list is called a mutator block and thus can be expanded or shrunk by clicking the blue button in the upper left corner. For a summary of mutators, check out the Mutators page
7. Plug each text block into the pick random item block. (Ideas for answers: http://en.wikipedia.org/wiki/Magic_8-Ball)
Blocks should look something like this:
You've got a Magic 8-Ball App! Now your app is fully functional and will predict the future with absolute certainty. Test out that this works, and then come back for some challenge tasks to make the app even more fun.
TEST: Emulator or Phone
Emulator: Click on the picture of the 8-Ball, you should see one of your answers displayed in the Label2.Text field, followed by the sound.
Phone: Tap on the picture of the 8-Ball, you should see one of your answers displayed in the Label2.Text field, followed by the sound.

Part Three: Shake the Phone, Get a Prediction + Hear a Sound

Even though you have a working Magic 8-Ball app, there is a way to make it even more fun. You can use the accelerometer component to make the phone respond to shaking instead of responding to a button click. This will make the app much more like a real Magic 8-Ball toy. Note: This part can only be done with an actual phone or tablet equipped with an accelerometer. If you are using an emulator, skip this part and go to Challenge 1 instead.
DESIGN: App Inventor
From the Sensors palette, drag over an AccelerometerSensor sensor component. Notice that it automatically drops down to the “Non-visible components” area of the Viewer window. This is the only new component you need, so go on over to the Blocks Editor to change your program.
BUILD: Blocks Editor
1. From the blocks drawer, click on AccelerometerSensor, then drag out the block for when AccelerometerSensor.Shaking.
2. Disconnect all of the blocks from inside the Button1.Click block and move them inside the AccelerometerSensor.Shaking block. NOTE: you can move whole sections of connected blocks by clicking on the uppermost or leftmost block and dragging it. The connected blocks will come with it.
3. Delete the Button1.Click block to keep your work area tidy.
The blocks should look something like this:
TEST: Phone/Emulator
Phone: When you shake the phone it should show an answer and play a sound.
Emulator: unfortunately, you can not simulate shaking the phone when using the emulator.
Package the App to Your Phone!
Your app would disappear if you were to disconnect your phone from the Blocks Editor. This is because the app is still stored on the App Inventor server and not on your phone. Follow these instructions to package your app to your phone or to make an ".apk" file that can be installed on any android phone. Or, if you want to make your app even cooler, try the challenges below.

Challenge 1: Make the Magic 8-Ball Speak

Instead of (or in addition to) making the prediction appear as text, can you make the 8-Ball speak it aloud? Hint: the text-to-speech component is under the Media palette in the Designer. Note: Most Android devices have the text-to-speech (TTS) capability, but if you have trouble getting the TTS component in App Inventor to work, you may need to find out how to install TTS and/or enable TTS on your device.

Suggestions for Further Exploration

⦁ Make the image rotate when the phone is shaken or have several images that the app rotates through while the phone is shaken. You could use this technique to make it look like the triangle piece inside the 8-ball window is surfacing. You could also make different images for different predictions and display the correct image for each prediction.
⦁ Make a similar app but for a different purpose. The phone could be used in place of dice or yahtzee letters. It could simulate a coin toss or a random number or color generator for investigating probability.
⦁ Ask end users to add choices to the list of predictions (See Make Quiz tutorial).
⦁ "Crowd source" for prediction choices: allow people to send text messages and have the app add them to the list.
⦁ Make the 8 Ball app a "server" so that anyone who sends a text to it will receive a text message prediction in return.
⦁ Complete change the list to humorous choices (e.g. an app for teacher to use when a student has an excuse for not doing homework), or for useful purposes like randomly selecting a name from amongst people in the class.
Done with Magic 8-Ball? Return to Curriculum (Teacher Resources) or Tutorials.

Scan the Sample App to your Phone

Scan the following barcode onto your phone to install and run the sample app.

Download Source Code

If you'd like to work with this sample in App Inventor, download the source code to your computer, then open App Inventor, click Projects, choose Import project (.aia) from my computer..., and select the source code you just downloaded.
Tutorial Version: App Inventor 2
Tutorial Difficulty: Basic
Tutorial Type: Game Accelerometer