Iittia caiRin Tii t

And back again:

A klutz, this has been translated.

Figure 7.5: Machine translation is still a work in progress.

To use this program, simply select the starting and target languages, and then start typing a phrase. As you type, the program will use the Google Translation web service to translate your text into and out of the target language.

To create this application, start with a "Hello, Android" application using these parameters:

Project name: Translate Build Target: Android 2.2 Application name: Translate Package name: org.example.translate Create Activity: Translate Min SDK Version: 8

Since this example will access the Internet to make a web service call, we will need to tell Android to grant us permission.

f \ Lost in Translation

When I first thought of this example, I imagined that it would be easy to get some hilarious results. Unfortunately (or fortunately, depending on your point of view), the Google service does a pretty good job with most languages. If you find any especially funny cases where the translator really flubs up, please post them on the discussion forum at the book's website (http:// pragprog.com/titles/eband3) for others to enjoy.

Add this line to AndroidManifest.xml before the <application> XML tag:

Download Translate/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

The layout for this example is a little more complicated than usual, so we'll use the TableLayout view. TableLayout lets you arrange your views into rows and columns, taking care of alignment and stretching the columns to fit the content. It's similar to using <table> and <tr> tags in HTML.

Download Translate/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height="fill_parent"> <TableLayout android:layout_width= "fill_parent" android:layout_height="fill_parent" android:stretchColumns="1" android:padding="10dip"> <TableRow>

<TextView android:text="@string/from_text" /> <Spinner android:id="@+id/from_language" /> </TableRow>

<EditText android:id="@+id/original_text" android:hint="@string/original_hint" android:padding="10dip" android:textSize= "18sp" /> <TableRow>

<TextView android:text="@string/to_text" /> <Spinner android:id="@+id/to_language" /> </TableRow>

<TextView android:id="@+id/translated_text" android:padding="10dip" android:textSize= "18sp" /> <TextView android:text="@string/back_text" /> <TextView android:id="@+id/retranslated_text" android:padding="10dip" android:textSize= "18sp" /> </TableLayout> </ScrollView>

In this example, we have six rows, each row containing one or two columns. Note that if there is only one view in a row, you don't have to use a TableRow to contain it. Also, it's not necessary to use android: layout_width= and android:layout_height= on every view like you have to with LinearLayout.

The Spinner class is a new one we haven't seen before. It's similar to a combo box in other user interface toolkits. The user selects the spinner (for example, by touching it), and a list of possible values appears for them to pick. In this example, we're going to use this control for selecting from a list of languages.

The actual list is stored as an Android resource in the file res/values/ arrays.xml:

Download Translate/res/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?> <resources>

<array name="languages">

<item>Bulgarian (bg)</item> <item>Chinese Simplified (zh-CN)</item> <item>Chinese Traditional (zh-TW)</item> <item>Catalan (ca)</item> <item>Croatian (hr)</item> <item>Czech (cs)</item> <item>Danish (da)</item> <item>Dutch (nl)</item> <item>English (en)</item> <item>Filipino (tl)</item> <item>Finnish (fi)</item> <item>French (fr)</item> <item>German (de)</item> <item>Greek (el)</item> <item>Indonesian (id)</item> <item>Italian (it)</item> <item>Japanese (ja)</item> <item>Korean (ko)</item>

<item>Latvian (1v)</item> <item>Lithuanian (1t)</item> <item>Norwegian (no)</item> <item>Po1ish (p1)</item> <item>Portuguese (pt-PT)</item> <item>Romanian (ro)</item> <item>Russian (ru)</item> <item>Spanish (es)</item> <item>Serbian (sr)</item> <item>S1ovak (sk)</item> <item>S1ovenian (s1)</item> <item>Swedish (sv)</item> <item>Ukrainian (uk)</item> </array> </resources>

This defines a list called languages that contains most of the languages recognized by the Google Translation API. Note that each value has a long name (for example, Spanish) and a short name (for example, es). We'll use the short name when passing the language to the translator.

Now let's start modifying the Translate class. Here's the basic outline:

Download Translate/src/org/example/translate/Translate.java

Une 1 package org.example.translate;

- import java.util.concurrent.ExecutorService;

- import java.util.concurrent.Executors; 5 import java.util.concurrent.Future;

import java.util.concurrent.RejectedExecutionException;

- import android.app.Activity;

- import android.os.Bundle; 10 import android.os.Handler;

- import android.text.Editable;

- import android.text.TextWatcher;

- import android.view.View;

- import android.widget.AdapterView; 15 import android.widget.ArrayAdapter;

- import android.widget.EditText;

- import android.widget.Spinner;

- import android.widget.TextView;

- import android.widget.AdapterView.OnltemSelectedListener;

- public class Translate extends Activity {

- private Spinner fromSpinner; private Spinner toSpinner; private EditText origText;

private TextView transText; private TextView retransText;

private TextWatcher textWatcher;

private OnItemSelectedListener itemListener;

private Handler guiThread; private ExecutorService transThread; private Runnable updateTask; private Future transPending;

©Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState);

setContentViewCR.layout.main);

initThreadingO;

findViews();

setAdapters();

setListeners();

After declaring a few variables, we define the onCreate() method starting at line 37 to initialize the threading and user interface. Don't worry, we'll fill out all those other methods it calls as we go.

The findViews() method, called from line 42, just gets a handle to all the user interface elements defined in the layout file:

Download Translate/src/org/example/translate/Translate.java

private void findViews() {

fromSpinner = (Spinner) findViewById(R.id.from_language); toSpinner = (Spinner) findViewById(R.id.to_language); origText = (EditText) findViewById(R.id.original_text); transText = (TextView) findViewById(R.id.translated_text); retransText = (TextView) findViewById(R.id.retranslated_text);

The setAdapters() method, called from onCreate() on line 43, defines a data source for the spinners:

Download Translate/src/org/example/translate/Translate.java

private void setAdapters() {

// Spinner list comes from a resource, // Spinner user interface uses standard layouts

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(

this, R.array.languages, android.R.layout.simple_spinner_item);

0 0

Post a comment