In this tutorial, you will create an app that allows people to enter food orders for a pizza party. Orders are stored in a Fusion Table, providing one easy-to-access place for the data. By reading from the table, the app can easily display the orders that have been entered.
A Fusion Table is a Google service to support the gathering, managing, sharing, and visualizing of data. Data is stored in multiple tables on Google's cloud. Individual tables can be merged (or fused) if they contain a common column, and they can be easily visualized on maps and in various kinds of charts and graphs. All of the data are stored in a public table that can be accessed via Google Drive, and allows different users to add information to the tables. Coupled with a location sensor, an App Inventor app could post periodic updates of each user's location to a public fusion table. Users could post notes to mark noteworthy locations. For example, a team of botanists could use a Fusion Table app to create an annotated catalog of the trees or plants within a certain geographical area. See the Google Documentation on Fusion Tables for more information.
This tutorial introduces:
- Using the FusionTables component
- Using a WebViewer component
This tutorial assumes you are familiar with the basics of App Inventor, including using the Component Designer to build a user interface, and using the Blocks Editor to specify the app's behavior. If you are not familiar with the basics, try stepping through some of the basic tutorials before continuing.
IF YOU DOWNLOAD THE SOURCE CODE you will need to supply your own API KEY, your own Table ID, and your own TABLE URL in the global variables in the blocks editor. This source code will not work without these additions.
Creating your own Fusion Table
Creating your own Fusion Tables is as easy as creating a Google document.
- On the web, login to your Gmail account or any other Google service (e.g., Drive, YouTube).
- Go to Google Drive and click the red New button and navigate to More. Select Google Fusion Tables in the list of choices. (If you do not see Google Fusion Tables in the menu, click +Connect more apps. Scroll down until you find Fusion Tables and hover over it. Click the + Connect button, then click OK. Now, when you click the New button, Fusion Tables should appear. If not, then you may need to contact your school's network administrator. See Troubleshooting section at the bottom of this page.)
- You will be given a few different options for the new table. Select Create empty table.
- You will see that the new table automatically comes with four columns. Change the column names for your pizza party app by going to Edit > Change Columns.You'll rename the four default columns to Date (type=Date), Name (type=Text), Pizza (type=Text), Drink (type=Text). Click save and then add a fifth column by going to the Edit > Add Column. Name this fifth column Comment (type=Text).
- Leave this window open so that you can come back and get the URL, which you'll need when you set up the properties of the WebViewer component in your app.
- Click on the Share button (top right) to modify the table's permissions. For this tutorial, you can specify a few friends who will use the app. Only people who are explicitly given permission will be able to enter pizza party preferences through your app. (See note box immediately below this.)
NOTE about Sharing Fusion Tables: To share a FusionTable with others, you must share it with each person individually, you can share with a Google Group, or you may share it with a service account the same way you would share a private Google Doc. There is no way to share write privileges to a FusionTable with the public. Public access is restricted to read-only. Please see the Setting up a Service Account section at the bottom of the page for how to set up a service account to authenticate all end-user access to your Fusion Table.
Getting an API Key
In order to use the FusiontablesControl Component you need to acquire a Google Applications Programming Interface (API) key. To get an API key, follow these instructions:
- Go to your Google Developers Console and log in with your Google account if necessary.
- On the left-hand menu, under APIs & auth select the APIs item.
- In the list of APIs, scroll down to find the Fusion Tables API and click the toggle button to "on" (if it already says "on" then leave it as is.)
- If prompted, read and agree to the terms of service. On the Developers Console API page you'll see that the on/off switch next to Fusion Tables API is now "On" and green.
- On the menu on the left of the screen select the Credentials item under "APIs & auth.
- Under Public API access click Create new Key, choose Android key and click Create to generate an API key. Don't worry about the pop-up box asking about SHA keys. Just click the "Create" button. Your API key will show up under "Public API Access". You will need set use this API key for the "API Key" property of the FusiontablesControl component in any app that you make that uses Fusion Tables. (More info below.)
Building the App
Connect to the App Inventor web site and start a new project. Name the new project PizzaParty, set the screen's orientation to Portrait and uncheck the Screen's scrollable property. You may also wish to set the Screen's Title property to something other than 'Screen 1'.
The User Interface
In addition to the FusiontablesControl component, the Pizza Party app makes use of several other types of components. It is assumed that you have learned how to use these in previous tutorials. Use the designer window to create the interface for the Pizza Party. When completed, the designer should look like this:
The components are:
|Component Type||Palette Group||What you'll name it||Purpose of Component||Settings of Component|
|Label||User Interface||LabelName||Shows the text "Your Name:"|
|TextBox||User Interface||TextBoxUserName||Gets input from user||Set the width property to Fill Parent|
|HorizontalArrangement||Layout||HorizontalArrangement1||Contains Name Label and Textbox|
|ListPicker||User Interface||ListPickerPizza||Accesses the list of available pizza types.||
Set the Width property to Fill Parent
Set the Text property to "What type of pizza?"
Set the ElementsFromString to "Cheese, Pepperoni, Anchovies, Hawaiian"
|ListPicker||User Interface||ListPickerDrink||Accesses the list of available drinks.||
Set the Width property to Fill Parent
Set the Text property to "What type of drink?"
Set the ElementsFromString to "Coke, Diet Coke, Sprite, Ginger Ale"
|Label||User Interface||LabelComment||Shows the text "Comments:"|
|TextBox||User Interface||TextBoxComments||Takes user input||Set the width property to Fill Parent|
|HorizontalArrangement||Layout||HorizontalArrangement2||Contains Comments Label and Textbox|
|Button||User Interface||ButtonSubmit||Adds new data to the public fusion table||
Set the Text to "Submit"
Set the width property to Fill Parent
|WebViewer||User Interface||WebViewer1||Displays Fusion Table||
Set width property to Fill Parent
Set height property to Fill Parent
|FusiontablesControl||Storage||FusiontablesControl1||Manages interactions with the app's Fusion Table|
|Clock||User Interface||Clock1||Used to provide a timestamp each time an order is placed.|
|Notifier||User Interface||Notifier1||Notifies the user of any errors|
Determining your Fusion Table URL and Table ID
In the blocks editor, you will set the WebViewer component's HomeURL property to point to the URL of your table. To find your Fusion Table's URL:
- In your browser, navigate to the Fusion Table you just created.
- Go to the menu and select Tools > Publish.
- You'll see a notice saying: "This table is private and will not be visible". Click the blue link that says "Change Visibility".
- In the list of "Who Has Access", click the blue "Change..." link next to "Private - Only people listed below..."
- Choose"Public on the Web" or "Anyone with the link". Either of these settings will work for this tutorial. In the future, you should decide this setting based on the sensitivity of your data.
- Click the green Save button, then the blue Done button.
- Back on the Fusion Table page, go to the menu bar and select Tools > Publish. Select the URL from the top text box (labeled "Send in an email or IM"), copy the URL and return to App Inventor. You will paste the URL into the definition block for the TABLE_URL (see below).
- You can find the Table ID by browsing to your table, then selecting File>About this table in the menu.
Open the Blocks Editor so you can program the app's behavior. First, you will describe the app's variables. Variables whose names are ALL_CAPS are constants -- that is, variables whose values do not change while the program is running. It is good to get into the habit of using this naming convention. Define the following variables and give them the initial values shown in the table.
|initialize global TABLE_URL||Variables||Initialize this global variable to the "published" URL of your fusion table. See instructions above.|
|initialize global TABLE_ID||Variables||Initialize this global variable to your table ID (e.g. a long string of characters unique to your fusion table). See instructions above.|
|initialize global API_KEY||Variables||Initialize this global variable to your own API Key for Google Fusion Tables. See instructions above.|
|initialize global UserName||Variables||Records the name of the user. (Starts off as an empty text string.)|
|initialize global pizza||Variables||Stores the pizza choice input by the user. (Starts off as an empty text string.)|
|initialize global drink||Variables||Stores the drink choice input by the user. (Starts off as an empty text string.)|
|initialize global comment||Variables||Stores the comment input by the user. (Starts off as an empty text string.)|
Initializing the App
It is important to perform some initialization steps whenever the app is started. These are done in the Screen1.Initialize block. For this app we need to set the initial values for the FusionTable component's API Key property (set to global API_KEY) and the WebViewer component's HomeURL property (set to global TABLE_URL). We also tell the app to forget the user's login credentials. This will prompt the user to login to their Google account and give permission to the app to access the Fusion Table. This authentication step will happen only once when the app first tries to access the Fusion Table. (Remember, by default, Fusion Tables are only writeable by users who have been given permission by the table's owner. You specify this in the Sharing settings for the Fusion Table which is easy to do from the Google Drive webpage. Also, see the Service Account Authorization note at the bottom of the page.)
Set up the resetForm procedure as shown below. After recording an entry, this procedure resets the interface back to the original state.
List Picker Blocks
In the designer, you set the choices for the pizza and drink types by filling in the "Selection" property with comma separated lists. These pre-programmed choices will be displayed on the user interface so the user can select their food and drink. Their selections are stored in the pizza and drink variables.
Once the user has entered their name, food choices, and comments, they will click the Submit button. The app tests to make sure that the name, pizza, and drink fields have values in them, and prompts the user to try again if any of the required answers are missing. Notice that the compare texts block is used (find it under Built-in palette, Text drawer). This block compares two strings of text to see if they are equal. If all required information is present, it calls the procedure InsertDataInTable (see below). The blocks for the ButtonSubmit.Click are shown here:
Inserting Data into the Fusion Table
The FusiontablesControl component is used to send the data to the Fusion Table. This action will create a new row in the Fusion Table, setting the values of the various columns involved. App Inventor makes this easy to do, but you have to be careful that the insert query is formatted correctly.
This procedure involves two steps: (1) constructing the insert query; then (2) sending the query to Google's Fusion Table service. The query we want to send will take this format:
- Clock Timer
- Data Storage