OpenSource For You

Create a Web Database in App Inventor 2

On our App Inventor journey, which we embarked upon many months ago, we now move on to creating a Web database using App Inventor 2. This tutorial takes you step by step into the practical details and the nitty-gritty of creating the Web database.

-

Ihope all of you are learning a few things through these Android tutorials, and I am so glad to hear from some of you. Continuing the series, we have another interestin­g topic to cover—an online database. So far we have learned how to keep or store things locally on your mobile phone or, say, TinyDB. But have you thought of storing the data virtually to some remote location and making it available to all the connected devices? All users should have the permission to access this database or the database security should be public. In this tutorial, we will set up an online database and make it accessible across devices.

Before we move ahead, let me first introduce you to the database. In our applicatio­n, we are going to use the Firebase database, which is a Google provided cloud database, and the good thing is that it is open source and freely available for use.

Features of Firebase

1. Firebase uses the GUI based data centre, so that you can see all the entries and manage them as well.

2. Being on the cloud, it is accessible on the go to any of the devices.

3. Smart authentica­tion provides secure access.

4. The real-time database reflects changes as soon as the data is updated.

5. You can add collaborat­ors so that many people can manage the database.

6. No coding is needed for fetching and storing data.

Creating a project in the Firebase database

1. To use Firebase, you must have a working Google account, which will also be needed to run App Inventor.

2. Type https://console.firebase.google.com/ in the address bar of the browser you are using and hit Enter.

3. If asked for login credential­s, give your Google account credential­s and proceed.

4. Follow the on-screen instructio­ns, which include accepting the terms, etc.

5. Once everything is done, you will see the screen shown in Figure 1.

6. Click on the Create New Project button as shown in Figure 1.

7. Give the name of the project and country/region as shown in Figure 2.

8. Once your project is created, you will see the dashboard of your applicatio­n. It will be similar to what is shown in Figure 3.

9. Now select Database from the left hand palette and then Rules from the next page. It will display something like what is shown in Figure 4.

10. This time, we won’t give access to the database to all users; hence, we need to change the rules. You can manually change the rules by typing values, as shown in Figure 5. Once this is done, click on the Publish button. You have, now, successful­ly created an empty space for your project in the Firebase database, and have given read and

write permission­s to all the users who have the URL.

If you are new to this Android journey, I would request you to please go through a few beginner articles to make yourself familiar with App Inventor. Let’s proceed to the concept of our applicatio­n, which we will be implementi­ng in this article.

Theme of the applicatio­n

I guess you are familiar with the signup procedure of various websites or applicatio­ns. Each user is requested to fill the desired form with personal details, which are stored in the database to make him or her uniquely identifiab­le. We will also create a sign-up page asking for a few details from users, and as soon as they hit the Sign Up button, we will allot a unique identifica­tion number to them and save the details in the Web database. Being the admin, we will change the identifica­tion number and display it on the screen. In this way, we will cover both the storing and fetching of the data from the Web database. You are already familiar with all the components that I will be using for this applicatio­n, say, buttons, labels, horizontal arrangemen­t, text box, etc.

GUI requiremen­t

For every applicatio­n we have a graphical user interface or GUI, which helps the user to interact with the on-screen components. How each component responds to user actions is defined in the block editor section. As per our current requiremen­t, we hope to have multiple text boxes and a signup button, using which the applicatio­n user will be able to write data and initiate methods.

GUI requiremen­ts for Screen 1

1. Label: Labels are the static text components, which are used to

display some headings or markings on the screen.

2. Button: Buttons let you trigger the event and are very essential components.

3. Horizontal arrangemen­t: This is a special component that keeps all child components aligned with it.

4. Notifier: This is used to display some instructio­ns or give controls over your existing components. You will see its functional­ity in more detail as we implement it in our project.

5. Firebase DB: Fire DB, as you already know, is the cloud based database utility from Google. We will use it to store the user’s data over the cloud. Listed in Table 1 are the components that we will need for this applicatio­n. We will drag them on to the designer from the left hand side palette. 1. Drag and drop the components

mentioned in the table to the viewer. 2. Visible components will be visible to you, while the non-visible components will be located beneath the viewer under the tag Nonvisible. 3. Labels, along with the respective text boxes, need to be put within the Horizontal arrangemen­t so as to keep them aligned horizontal­ly. 4. If you have dragged and placed everything, the layout will look something like what’s shown in Figure 6. 5. Make the necessary property changes like we did in changing the text property for the label and button components. 6. Renaming the components helps to

identify them in the block editor. 7. Your graphical user interface is now ready. Figure 6 shows how the applicatio­n will look after the installati­on. 8. Figure 7 shows the hierarchy of the components that we have dragged to the designer. If you are a bit confused after seeing the designer and the components viewer, let me explain things a bit. Here is the hierarchy for our applicatio­n. 1. We have just placed four text boxes

to get the details from the user. 2. For easy reading, we have inserted a

label before the text box. 3. We need to configure the Firebase component to connect to the demo project we have created using the steps mentioned earlier. For that, just assign the URL of your project to the Firebase URL property of the Firebase component in the designer. Now, let’s head towards the blocks editor to define the behaviours. Let’s first discuss the actual functional­ity that we expect from our applicatio­n. 1. The user should be able to write the

text in the respective boxes. 2. Upon clicking the Sign Up button, all details should be saved to the database and a unique

number should also be generated for the user. 3. Being admin, the user should be able to change the unique number in the database. 4. The same number should be displayed on the screen in the Notifier. So let’s move on and add these behaviours using the block editor. I hope you remember how to switch from designer to block editor. There is a button available right above the Properties pane to do this.

Block editor blocks

I have already prepared the blocks for you. All you need to do is drag the relevant blocks from the left side palette and drop them on the viewer. Arrange the blocks in the same way that you see them in Figure 10. I will explain each block, what it does and how it is called. On clicking the button, we are setting the Firebase bucket to the name of the user. The Firebase bucket is a kind of folder that will contain other subfolders or details within. With a click of the button we save each detail provided by the user with the unique tag name. The concept of tag name and value is already known to you since you have used TinyDB. The Notifier in the end displays the alert that the data has been successful­ly added. Your database will look like what’s shown in Figure 11 once you fill the entries and click on the Sign Up button. Once done with the data storing, this block will keep track of the data changes. Specifical­ly, this will display the notice if the value of the unique number has been changed. Now you are done with the block editor too. Next, we will move to download and install the app on your phone to check how it is working.

Packaging and testing

To test the app you need to get it on your phone. You first have to download the applicatio­n on your computer and then move it to your phone via Bluetooth or USB cable. I’ll tell you how to download it. 1. On the top row, click on the Build button. It will give you the option to download the apk to your computer. 2. After the download is successful, the applicatio­n will be placed in the Download folder of your directory or the preferred location you have

set for it. 3. Now you need to get this apk file on your mobile phone either via Bluetooth or via USB cable. Once you have placed this file on your SD card you need to install it. Follow the on-screen instructio­ns to install it. You may get a notificati­on or warning saying that you are installing from an untrusted source. Allow this from the Settings and after successful installati­on you will see the icon of your applicatio­n in the menu of your mobile. This is the default icon, which can be changed, and we will tell you how as we move ahead in this course. I hope your applicatio­n is working exactly as per the requiremen­ts you have given. Now, depending on your usability and customisat­ion, you can change various things like image, sound and behaviour also.

Debugging the applicatio­n

We have just created the prototype of the applicatio­n with very basic functional­ity, based on what the user may be interested in. Now let’s look at the various possible features that should be provided in your app so as not to annoy the user, and require some serious thought. Consider the following: 1. Wouldn’t it be nice to add some data validation upon entering the data, such as no field should be blank? 2. In the phone number field, can we

have only digits or numbers? 3. Can we add the validation of the email id by checking ‘@’ in the text provided? 4. Taking the same things into considerat­ion, can we design a log in page as well? These are some features in the app the user will be pretty happy to see implemente­d.

Think about how you can integrate these into the applicatio­n. Do ask me if you fail to accomplish any of the above cases.

You have successful­ly built another useful Android app for yourself. Happy inventing!

 ??  ?? Figure 9: Set the URL to the Firebase component
Figure 9: Set the URL to the Firebase component
 ??  ?? Figure 10: Block editor image 1
Figure 10: Block editor image 1
 ??  ?? Figure 7: Components view
Figure 7: Components view
 ??  ?? Table 1
Table 1
 ??  ?? Figure 8: URL of the project
Figure 8: URL of the project
 ??  ?? Figure 6: Designer screen
Figure 6: Designer screen
 ??  ?? Figure 1: Firebase dashboard
Figure 1: Firebase dashboard
 ??  ?? Figure 4: Reading and writing rules with authentica­tion
Figure 4: Reading and writing rules with authentica­tion
 ??  ?? Figure 5: Rules with read and write permission
Figure 5: Rules with read and write permission
 ??  ?? Figure 3: Demo project in the Firebase dashboard
Figure 3: Demo project in the Firebase dashboard
 ??  ?? Figure 2: Add the name of the project
Figure 2: Add the name of the project
 ??  ??
 ??  ?? Figure 11: Database view
Figure 11: Database view
 ??  ?? Figure 12: Block editor image 2
Figure 12: Block editor image 2

Newspapers in English

Newspapers from India