No Text While Driving

Download Book Chapter

This tutorial demonstrates how an app can respond to text messages automatically. You'll build an app that sends back a response when a text message is received. The idea for the app came from University of San Franciso student Daniel Finnegan.

This tutorial assumes 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.

Getting Started

Connect to the App Inventor web site and start a new project. Name it NoTextWhileDriving, and also set the screen's Title to NoTextWhileDriving. Open the Blocks Editor and connect to the phone.


You'll design the app so that it sends a response to any text message received. You'll also allow the user to customize the response sent.

The tutorial introduces the following App Inventor concepts:

  • The Texting component for sending texts and processing received texts.
  • The TinyDB database component for saving the customized message even after the app is closed.

Set up the Components

The user interface for NoTextWhileDriving is simple: it has a text box for the response message and a button for submitting a change to this message. You'll also need a Texting component and a TinyDB component, both of which will appear in the "non-visual" component area. A table of detailed instructions for designing the components is below, but you can also build it from the following picture of what it should look like:

The components listed below were used to create the designer window shown above. Drag each component from the Palette into the Viewer and name it as specified below:

Component Type Palette Group What you'll name it Purpose of Component
Label Basic PromptLabel Let the user know how the app works
TextBox Basic MessageTextbox User will enter custom response here.
Button Basic
SubmitResponseButton User clicks this to submit response
Texting Social Texting1 The component that processes the texts
TinyDB Basic TinyDB1 The component that will store the response in the database

Set the properties of the components in the following way:

  • Set the Text of PromptLabel to "The text below will be sent in response to all texts while this app is running."
  • Set the Text of MessageTextbox to "I'm driving right now, I'll contact you shortly".
  • Set the Text of SubmitResponseButton to "Modify Response" .

Add behaviors to the components

NoTextWhileDriving has the following behaviors:

1. When a text is received, the message entered in MessageTextbox is sent as a text message response to the sender.

2. When the user modifies the custom message in MessageTextbox and clicks the SubmitResponseButton , the new message is saved persisently in the phone's database.

3. When the app begins, the custom message is loaded from the database into MessageTextbox .

Responding to a text

When a text message is received by the phone, the Texting.MessageReceived event is triggered. Your app should handle this event by setting the PhoneNumber and Message properties of the Texting component appropriately and sending the response text.

You'll need the following blocks:

Block Type Drawer Purpose
Texting1.MessageReceived Texting1 the event-handler that is triggered when phone received a text
set Texting1.PhoneNumber to Texting1
set the PhoneNumber property before sending
value number My Definitions this is the phone number of the person who sent the text
set Texting1.Message to
Texting1 set the Message property before sending
MessageText.Text MessageText This is the message the user has entered.
send the message

The blocks should look like this:

How the Blocks Work

When the phone receives any text message, the Texting1.MessageReceived event is triggered. The phone number of the sender is in the argument number, and the sender's message is in the argument messageText .

In response, the app sends a text message to the sender. To send a text, the app sets the two key properties of the Texting component: PhoneNumber and Message . Texting.PhoneNumber is set to the number of the sender, and Texting.Message is set to the text in MessagetextBox -- this might be the default, "I'm driving right now, I'll contact you shortly.", or the user may have modified it. Once these properties are set, Texting1.SendMessage is called to actually send the response text message.

Test the behavior. You'll need a second phone to test this behavior. From the second phone, send a text to the phone that is running the app. Does the second phone receive the response text? If that works, try modifying the response message and sending another message from the second phone. Is the new response sent?

Storing the custom response

The app so far works, but if you close the app and reopen it, the custom message will be lost. To make things more convenient for the user, store the custom response message they enter into a database using the TinyDB component.

TinyDB provides two blocks: StoreValue and GetValue . The former allows you to store a tagged piece of information, while the latter let's you retrieve one.

You'll need the following blocks to store the custom message:

Block Type Drawer Purpose
SubmitResponseButton.Click SubmitResponseButton user clicks this button to submit new response message
store the custom message in the phone's database
text ("responseMessage") Text use this as the tag for the data
MessageTextbox.Text MessageTextbox the response message entered by the user is here

The blocks should look like this:

How the Blocks Work

When the user clicks the SubmitResponseButton , the app will store the response message entered by the user in the database. The text "responseMessage" is used as a tag to uniquely identify the information-- later, you'll use that same tag to retrieve the message from the database.

Retrieving the saved response message

Program the Screen1.Initialize event-handler so that the saved custom response message is retrieved from the database and placed in MessageTextbox . Check the retrieved data to make sure there's something there-- after all, the first time the app is used, the database will not have any message saved. If there is a stored message, place it in the MessageTextbox so that the user can see it and so that it will be used to respond to incoming texts.

You'll need the following blocks:

Block Type Drawer Purpose
def variable ("response")
A temporary variable to hold the retrieved data
text (blank) Text Initial value for the variable can be anything
Screen1.Initialize Screen1 This is triggered when app begins
set response to My Definitions you'll set the variable to the value retrieved from db
TinyDB1.GetValue TinyDB1 get the stored response text from the database
text ("responseMessage") Text plug into tag slot of GetValue , make sure text is same as was used in StoreValue above
if test Control to ask if the retrieved value has some text
> block Math check if length of retrieved value is greater than (>) 0
length text Text check if length of retrieved value is greater than 0
global response My Definitions this variable holds the value retrieved from GetValue
number (0) Math to compare with length of response
set MessageTextbox.Text to MessageTextbox if we retrieved something, place it in MessageTextbox
global response My Definitions this variable holds the value retrieved from GetValue

The blocks should look like this:

How the Blocks Work

When the app begins, the Screen1.Initialize event is triggered. The app calls the TinyDB1.GetValue with a tag of "responseMessage"-- the same tag used when you stored the user's entry earlier. The resulting value is placed in the variable response.

The variable response is used so that the value returned from the database can be checked. If it has a length of 0, then there was no database entry with a tag of "responseMessage"-- something that will occur the first time a user runs this app. But if the length is greater than 0, the app knows that a custom response has been stored previously, and the retrieved value can be placed in the MessageTextBox which the user will see and which is used as the message for any response texts sent.

Test the app. Enter a new response message in the MessageTextbox and click the SubmitResponseButton . Then restart the app by clicking the Restart App button in the Blocks Editor. This will cause the app to re-initialize just like it will when a user closes the app and reopens it later. Does the custom message you entered appear?

No Text While Driving, Final Program


Once you get the No Text While Driving app working, you might want to explore some variations. For example,

  • Write a version that speaks the received texts aloud. You'll need to use the TextToSpeech component.
  • Write a version that lets the user define custom responses for particular phone numbers.
  • Write a version that sends custom responses based on the user's location (e.g., I'm in church...)


Here are some of the ideas covered in this tutorial:
  • The Texting component can be used both to send text messages and process the ones that are received.
  • The TinyDB component is used to store information persistently, in the phone's database, so that it there each time the app is opened.

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, go to the My Projects page, and choose More Actions | Upload Source .
Google is grateful to Professor David Wolber , CS Professor at The University of San Francisco, for developing this tutorial.