Building a mobile application

In the previous section, we understood the frontend of an HTML page that would translate input for us. In this section, we will build the frontend of an Android app that leverages the endpoint we generated for the function to return the translated text for us.

We create the layout of the app as follows:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="56dp"
android:text="Translate"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input" />

<EditText
android:id="@+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="84dp"
android:layout_marginTop="84dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/out"
android:layout_width="197dp"
android:layout_height="80dp"
android:layout_marginStart="92dp"
android:layout_marginTop="56dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</android.support.constraint.ConstraintLayout>

The preceding code would output the layout of the app:

Note that we have an EditText view that takes input.

The Button is used to execute the translation and out is the TextView where the output is displayed.

Also note that in the preceding code, we have ensured that the components are aligned to the screen.

Within main activity, we execute the following code:

package com.example.admin.translateapp;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends AppCompatActivity {
public String myurl;
public String result;
public String response;
public EditText inp;
public TextView out;
public Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inp = (EditText) findViewById(R.id.input);
out = (TextView) findViewById(R.id.out);
btn = (Button) findViewById(R.id.button);
myurl = "http://us-central1-mytranslator-c656d.cloudfunctions.net/translateMessage?text=";

btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RequestTask task = new RequestTask();
task.execute(inp.getText().toString());
}
});
}

private class RequestTask extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... uri) {
try {
URL url = new URL(myurl+uri[0].toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader streamReader = new
InputStreamReader(conn.getInputStream());
//Create a new buffered reader and String Builder
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
//Check if the line we are reading is not null
String inputLine;
while((inputLine = reader.readLine()) != null){
stringBuilder.append(inputLine);
}
//Close our InputStream and Buffered reader
reader.close();
streamReader.close();
//Set our result equal to our stringBuilder
result = stringBuilder.toString();
//result = conn.getResponseMessage();
} else {
}
} catch (IOException e) {
//TODO Handle problems..
}
return result;
}

@Override
protected void onPostExecute(String result1) {
super.onPostExecute(result1);
out.setText(result1);
}
}
}

Let us understand the preceding code.

Import the relevant packages:

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

Initialize the objects that we use in the MainActivity class:

public String myurl;
public String result;
public String response;
public EditText inp;
public TextView out;
public Button btn;

Also, initialize the views using the following code:

inp = (EditText) findViewById(R.id.input);
out = (TextView) findViewById(R.id.out);
btn = (Button) findViewById(R.id.button);

Set the on-click listener:

btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RequestTask task = new RequestTask();
task.execute(inp.getText().toString());

Specify the tasks that need to be executed when clicked on the button:

URL url = new URL(myurl+uri[0].toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader streamReader = new
InputStreamReader(conn.getInputStream());
//Create a new buffered reader and String Builder
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
//Check if the line we are reading is not null
String inputLine;
while((inputLine = reader.readLine()) != null){
stringBuilder.append(inputLine);
}
//Close our InputStream and Buffered reader
reader.close();
streamReader.close();
//Set our result equal to our stringBuilder
result = stringBuilder.toString();

From the preceding code, the URL gets evaluated to the URL that we have seen in the previous web application section.

The output of the preceding code is as follows:

Note that, on clicking the BUTTON, we should be able to translate our text.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.172.243