Android 001: Hello World App
This will help you:
Create, test and install your first Android app
Android App Inventor is an online program which allows you to create, test and install Android apps using a block-based coding language. This means you don't have to worry as much about knowing what to type to make something happen - you can just look for a block that does what you need, and drag and drop it into your code. In this activity, you'll create a VERY simple Android app while familiarizing yourself with App Inventor. If you have an Android device handy, you can even test or install it.
Time: 1-2 hours / Level: A1
You should already:
Have an Android device to test on (optional)
Step 1: App Inventor for Android
Go to the App Inventor for Android website. Click on the "Create Apps" button in the upper right corner. Create an account or sign in using a Google account if you prefer. After that, you should be taken to the "My Projects" page.
Step 2: Creating a Project
The "My Projects" page shows buttons to start, delete and publish projects near the top, with any current projects you have listed underneath. At the very top of the page, there are drop-down menus that allow you to further manage projects, connect to an app simulation, build a packaged app, and get help.
From the "My Projects" page, click on "Start new project" and enter a name. Choose a name that makes sense, but don't worry - you can change the app's name later. The project will appear in your list of projects. Click on it to open the editor.
Step 3: Screen Layout Editor
The screen layout editor, or "Designer", is where you can change the appearance of your app, add pages or screens, add components, and edit some of the main details of your app, like its name and icon.
The left side has expanding panels of components that you can add. The "User Interface" panel has some basic components that the user might see and interact with. The "Layout" panel has containers that arrange components in a certain way and allow scrolling. You should explore the other panels to see what App Inventor can do. See if you can find components for:
Playing a song
Converting speech to text
Translating text
Taking a video
Having a character sprite move around
Detecting location
Scanning a barcode
Sending a text
Saving a file to the device
Saving some app data to the device
Saving data to an online database
Send data over Bluetooth
Notice that a lot of these components aren't something you can see, but you need to drag them into the display area for them to be included.
The right side of the screen has a "Components" panel and a "Properties" panel. The "Components" panel shows a tree of all the components you have. You can select, rename and delete components from here. Right of that, the "Properties" panel will show all kinds of changeable properties for any selected component. In the properties for the first screen, you can change the app name and icon, whether the title shows at the top, and a lot more about how the app appears.
Once you get familiar with the layout, you can start building an app. Our first app will just have a button that makes some text display. Drag a button and a label on to the model phone screen to add them. For bonus points, start with a Layout component of some sort, and add the button and label inside it.
Go over to the "Components" panel, click each of your components, and click "Rename" at the bottom. Rename them to something short that makes sense - for now, the name doesn't matter, but adding components without naming them should be illegal because when you have several, say, buttons, you need to know what they're each for!
Next, select the button and go to its Properties. Near the bottom, there is a box labeled "Text", where you can set the text on the button. Change it to something, like "Next screen", or leave it blank. While you're here, try changing the button height, width, shape, or color. You can make the button un-clickable ("Enabled"), choose if it changes appearance when clicked ("ShowFeedback"), or make it invisible at startup ("Visible").
Finally, select the label and change the text and other properties however you want to. It can be the beginning of a joke, a greeting, an instruction to click the button... whatever.
At the top of the page, to the right of the app name, is a bar to manage screens. Screens are like pages of your app. They each have their own components and importantly, their own code. Add a screen and call it "NextScreen". After a second, the designer should automatically switch over to your next screen. In this screen, add whatever you want - maybe an image and a text label, or a music player.
Play with the properties of your chosen components, and maybe arrange them in a Layout component. Lastly, add a button, name it "BackButton", and set the text on it to "Back".
In this example, a vertical arrangement contains some HTML formatted content (yes, that's an option) in a label component above a bar containing the back button.
In this app, we want to start on the first screen with some text, then click a button to go to the next screen. That next screen will have whatever content you put on it, and a back button that goes back to the first screen. Since you have all the components you need and you know what they need to do, it's time to code!
In the upper-right corner, click on the "Blocks" button (next to "Designer") to get to the blocks editor.
Step 4: Code Blocks Editor
In the blocks editor, you work on the code for one screen at a time. You get code blocks from the expanding menus in the left panel, and drop them into the editing area in the middle. The "Built-in" section contains code elements that can be used regardless of components. They do the logical, programmatic steps of the code (like doing math, making decisions, repeating steps, and remembering values.) Below the "Built-in" blocks are block specific to the components on a screen. They handle things like appearance of the components and triggered actions.
Click through some of the menus of blocks. If you are familiar with programming, you may recognize some of the instructions on the blocks as similar to commands and structures in programming.
Some of the blocks have "mouths" or "overhangs"; these blocks are intended to have other instruction blocks placed inside them. Some blocks have notches in the top or bumps on the bottom; these are able to have other instruction blocks happen before or after them. Some have puzzle-piece-like slots; this means that an answer block (with a puzzle-piece tab) is required to go there.
If a block is resisting "snapping" into another, it might be because it can't work there. This happens a lot with the puzzle-piece answer blocks. For example, if you try to snap a text block (they're pink) into a math problem, it won't work. Because text is the wrong type of information, it can't be used.
So, basically, blocks fit together how they look like they should. You can stack instruction blocks, and stick answer blocks into them when needed. You can put one or more instruction blocks into a block with a "mouth". Generally, instruction blocks do something, and answer blocks can be used to get information. Some blocks do both.
Okay, that's a run down of how blocks work. You should click around the menus and see what's where. Try to figure out, roughly, what 2-3 blocks from each section do. It'll help you locate similar ones.
Finally, let's make your app work. In the upper left, there's still a drop-down to choose the screen. Select "Screen 1" or the name of your starting screen. Here's what needs to happen on Screen 1:
When the Next button is clicked:
Open the next screen
This method of writing out pseudocode, then finding blocks that roughly match up, is a great way to create code. It separates the task of thinking about steps from the task of expressing those steps in a certain language, so it makes writing code more efficient, easier, and safer from mistakes.
The button click trigger is under the menu for the button ("NextButton"), and a screen opening command is under the "Control" section. The puzzle piece slot means it needs to know the name of the screen to open, so we can grab a text box from the "Text" section to answer that question. Just type in the exact name of your next screen, and snap it in.
Your code should look like this:
Now, choose the next screen from the screen drop-down in the upper left. On this screen, you were required to add a button, but the rest of the content could be whatever you want. So, we'll talk about how to make the button work, but the rest will be a coding playground for you to experiment with whatever components you choose.
The button operation is almost identical to the code from the start screen. Here's the pseudocode:
When the Back button is clicked:
Open the start screen