Appinventor Programming
Today we will see with two appinventor apps, which will give us a chance to work with:
- variables
- lists
- ListPickers in appinventor's interface
- appinventor's Notifies
"Did you Exercise?" App
In this app, the user is presented with a question:
"Did you exercise today?":
and a list of possible answers ("YES, and "NO"):
The user can presumably answer this question every day, over a period of time.
When the user decides to get the results from this activity, she will see something like this:
This app is already programmed for you. Here is the appinventor code for this app:
Initializations code above:
and the rest of the code (event handlers):
Discussion points on the code above
Notice the definition of the variable
choicesList, which holds a list
of
two elements, each being a piece of text.
The choicesList
is used to initialize the ChoicesPicker
, as soon
as the screen opens (on the "Initialize" event of Screen1 component).
The variables numYES
and numNO
will eventually hold the number of the positive and negative answers,
respectively.
The variable currentAnswer
will hold the answer of the user every time she answers the question.
Notice how this variable gets its new value right after the user has picked an answer
("AfterPicking" event handler, in the "ChoicesPicker" component.) Next, the program is
using an ifelse
statement to act based on what the user's
current answer was: if it was "YES", then the counter for the positive answers is incremented by one.
In the opposite case, the counter for the negative answers is incremented.
When the ResultsButton is clicked, the ResultsButton.Click
events handler is executed.
At that time, the total number of answers (numYES + numNO) is computed, and displayed, together
with the number of positive and negative answers.
"Time Spent OnLine" App
In this task we will create anappinventor app that will track the time the user is spending
online on a daily basis. After some days of recording, the user can get the total number of
hours she has spend online over that period. In addition, she will see the time spend on line
from the first day of recording to the present.
Create the interface
We will create a basic interface for this app:
- Use a label to present what the app is doing.
- Add a ListPicker (rename it "ChoicePicker"), which will be populated with the possible
answers later, in the Blocks editor.
- Add a button, named "ResultsButton", which will trigger the presentation of the
results, when the user clicks on it.
- Finally, add three labels, named "ResultsLabel1", "ResultsLabel2" and "ResultsLabel3",
which will hold the results.
Program the app
In this case, we will need to maintain the user's choices over several days. For this we
will use a List
. Open the BlocksEditor.
Initializations
- Create a list of numbers (30, 60, 90 and 120 for example) which will be the choices presented
to the user (mins of online time per day). Create a variable, "choicesList", and assign to it the list of numbers.
- Arrange so that the Elements of the "ChoicesPicker" is set to "choicesList"
as soon as Screen1 initializes.
- Define a variable, named "resultsList", which will be initialized to an empty
list, which will hold the user's choices as a sequence of numbers.
ChoicesPicker.AfterPicking event handler
- Once the user has selected her choice, assign i to the variable "currentAnswer".
- Then add it to the "resultsList".
- Use a
Notifier
to show the currentChoice for testing purposes.
ResultsButton.Click event handler
- How can you figure out the number of days has the user recorded data? Show this number
in the "ResultsLabel1".
- How can you compute the total time the user has spent on-line? Hints: define a variable to hold this result.
What should it be its initial value? Check out the "foreach" block, in the Build-In --> Control drawer.
- Compute the total time
in hours
, and present it on the "ResultsLabel2".
- Lastly, think about presenting the sequence of the user's choices over the whole period of the recording time.