How to Build a Raspberry Pi Android App in 7 Steps

Original idea and source via Instructables.

Building an Android app isn’t always easy. There is so much that goes into an app and you don’t always have the time or the resources to build your app. For this tutorial, we want to share instructions for a simple Raspberry Pi Android app that you can build without any technical hassle. So let’s get started.

Note: this tutorial uses JSON to exchange data between the client and the server.

Step 1: Gather the parts

Photo Credit: Instructables.

For this project, you will need:

⦁ Your Android phone or Android emulator (from Android Studio)
⦁ A Raspberry Pi
⦁ An internet connection to your Raspberry Pi. Make sure to forward port 80 to the IP of Pi.
⦁ A Windows 8.1 or higher

Step 2: Get the software for windows

Photo Credit: Instructables.

You will need separate software for your Raspberry PI and Windows. For the computer, you will need Google’s Android studio. See the screenshot below:

Step 3: Get the software for Raspberry Pi

For the Raspberry Pi, you’ll need:

⦁ PHP
⦁ Apache 2
⦁ My SQL
⦁ PHPMyAdmin

Step 4: Create your database with PHPMyAdmin

Photo Credit: Instructables.

Follow the steps below to create a database:

  1. Open a web browser and go to IP_RPi/PHPMyAdmin.
  2. log in to PHPMyAdmin.
  3. Click databases, and create your new database. You can name it “parts”.
  4. Open this newly created database “parts” and create a new table with 3 sections.
  5. Add the following information in the 3 sections:
First column:
column: ID
Type: INT
Auto increment: v

Second column:
column: Name
Type: VARCHAR
Length: 30

Third column:
column: part_nr
Type: INT
  1. Press save and you have a new database ready.

Step 5: Add server-side code

On your Raspberry Pi, create 4 different files as mentioned below:

UPDATE: Place them in /var/www/html on raspbian Jessie.

db_config.php
db_connect.php
db_create.php
db_view.php

⦁ The config.php file will store the database name, username, and password.
⦁ The connect file establishes the connection to the database specified in the config file.
⦁ The created file creates a new row in the database table.
⦁ The view file fetches all the data in the database.

Step 6: Create the layout for the app


This step will help you build the layout for the app. Next, we will talk about how to make that app function by making it communicate with the Raspberry Pi server.

First, let’s start with the layout in Android studio.

Photo Credit: Instructables.

Give your new app a catchy name and assign it to a directory. Press next.

Photo Credit: Instructables.

Choose the devices for the app. For example, for this project, all phones with Android 4.0.3 and higher would work great.

Photo Credit: Instructables.

Next, choose the “blank activity” and leave the last screen as is.

Photo Credit: Instructables.
Photo Credit: Instructables.

After you are done with these initial steps, you will get the “hello world” example app.

Photo Credit: Instructables.

Go to main _activity.xml and head to the right bar, here clear the textview element.

Now go to values/strings.xml and remove “Hello World”.

Go back to the main-activity.XML and do the following:

  1. Drag a vertical linear layout in the top left corner.
  2. Add two text fields: first should be plaintext and the other should be number.
  3. Now scroll down and spot “id”. Now change this so that the first one is NAME and the second one is PART_NR.
  4. Now scroll down until you find “hint”. Here too, change the first one to NAME and the second one to PART NUMBER.
  5. Now add a button to the screen and change “text” to “send” and you are done with the layout.

See the following screenshots to understand better:

Photo Credit: Instructables.
Photo Credit: Instructables.
Photo Credit: Instructables.
Photo Credit: Instructables.
Photo Credit: Instructables.

Step 7: Write the Java code

All Android apps are written in Java, so in this project the code used is Java. When you build an app, you need a code to make its layout work. The layout is like the skeleton and the code is like the whole body with molecules and organs.

See the screenshot below:

Photo Credit: Instructables.

You can use the JSON library for this project. First, download the file here. Add the file in:

directiory/app_name/app/src/main/java/company/app_name/JSONParser.java

Next, open Android Studio and go to JASONParser. Here change the first text to:

package company.app_name;

Next, we’ll write the main code in Mainactivity.java

Create a new progress-dialogue that will display code execution time.

Now add this piece of code to the main class:

Private ProgressDialog pDialog;

Next, add this code under the above code:

JSONParser jsonparser = new JSONParser();

Next, add the following code under the last code to extrude the text:

EditText editText;
EditText editText2;

After that move to oncreate function and link edittexts above to edittexts in the layout:

editText = (EditText) findViewById(R.id.Name);
editText2 = (EditText) findViewById(R.id.part_nr);

In the next step, you create a new class to connect the server and transmit previously typed data.

Here is the class Create_part:

class Create_Part extends AsyncTask {
@Override protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Sending part to the database…"); //Set the message for the loading window
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show(); //Place the loading message on the screen
}
@Override protected String doInBackground(String… args) {
String String_name = editText.getText().toString(); //Get the inserted text from the editText files
String Int_Part = editText2.getText().toString();
List params = new ArrayList<>();
params.add(new BasicNameValuePair("Name", String_name)); //Add the parameters to an array
params.add(new BasicNameValuePair("part_nr", Int_Part));
// Do the HTTP POST Request with the JSON parameters
// Change "RaspberryPi_IP to your home IP address or Noip service
JSONObject json = jsonParser.makeHttpRequest("RaspberryPi_IP/db_create.php", "POST", params);
try {
int success = json.getInt("success");
if(success == 1){
Intent i = new Intent(getApplicationContext(), ConfirmActivity.class);
// Open a new activity to confirm it was sent. We're going to create it later.
startActivity(i);
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url){
pDialog.dismiss(); // Close the loading window when ready
}
}

Next, create a new function “send” and call Create_Part as shown below:

public void Send(View view) {
new Create_Part().execute();
}

In the activity_main.xml, change the onclick action (of the button) to “send”.

For the final steps, change activity name to “ConfirmActivity” and click finish.

Now go to strings.xml, and change “Hello World” to “The Part was Created”.

That’s it! Rebuild and test the app on your phone.

You can buy a Raspberry Pi here.

We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.

Source via Instructables.

4 thoughts on “How to Build a Raspberry Pi Android App in 7 Steps

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: