Extends from previous exercise, AndroidTranslate, using Google Translate API in Android application. It's a multi-language translation application. There are two spinner in the application, user can select the language to be translated.
------------------------------------------
Please note:
- google-api-translate-java have to be downloaded and build path have to be set, refer the article google-api-translate-java.
- "android.permission.INTERNET" have to be set in AndroidMainfest.xml, refer to the article AndroidTranslate, using Google Translate API in Android application.
------------------------------------------
Modify main.xml to add two Spinner to select the language to be translated from and to.
main.xml can be downloaded here.
Implement a file arrayLanguage.xml in the folder /res/values/
arrayLanguage.xml can be downloaded here.
Modify AndroidTranslate.java
AndroidTranslate.java can be downloaded here.
In this exercise, two spinner are used to select the language to be translated from and to. A array-string are used to store the languages.
It can be retrieved using:
(String)spinner_InputLanguage.getItemAtPosition((int) spinner_InputLanguage.getSelectedItemId())
(String)spinner_OutputLanguage.getItemAtPosition((int) spinner_OutputLanguage.getSelectedItemId())
It's same as the parameters to be passed to Translate.execute(), but in String type, not the expected Language type. So we have to use the Language.valueOf() to convert it to Language type.
------------------------------------------
Please note:
- google-api-translate-java have to be downloaded and build path have to be set, refer the article google-api-translate-java.
- "android.permission.INTERNET" have to be set in AndroidMainfest.xml, refer to the article AndroidTranslate, using Google Translate API in Android application.
------------------------------------------
Modify main.xml to add two Spinner to select the language to be translated from and to.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/InputText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="from:"
/>
<Spinner
android:id = "@+id/spinner_InputLanguage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="to:"
/>
<Spinner
android:id = "@+id/spinner_OutputLanguage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/TranslateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Translate"
/>
<TextView
android:id="@+id/OutputText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
main.xml can be downloaded here.
Implement a file arrayLanguage.xml in the folder /res/values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="language">
<item>ARABIC</item>
<item>BULGARIAN</item>
<item>CATALAN</item>
<item>CHINESE</item>
<item>CHINESE_SIMPLIFIED</item>
<item>CHINESE_TRADITIONAL</item>
<item>CROATIAN</item>
<item>CZECH</item>
<item>DANISH</item>
<item>DUTCH</item>
<item>ENGLISH</item>
<item>FILIPINO</item>
<item>FINNISH</item>
<item>FRENCH</item>
<item>GERMAN</item>
<item>GREEK</item>
<item>HEBREW</item>
<item>HINDI</item>
<item>INDONESIAN</item>
<item>ITALIAN</item>
<item>JAPANESE</item>
<item>KOREAN</item>
<item>LATVIAN</item>
<item>LITHUANIAN</item>
<item>NORWEGIAN</item>
<item>POLISH</item>
<item>PORTUGESE</item>
<item>ROMANIAN</item>
<item>RUSSIAN</item>
<item>SERBIAN</item>
<item>SLOVAK</item>
<item>SLOVENIAN</item>
<item>SPANISH</item>
<item>SWEDISH</item>
<item>UKRANIAN</item>
<item>VIETNAMESE</item>
</string-array>
</resources>
arrayLanguage.xml can be downloaded here.
Modify AndroidTranslate.java
package com.exercise.AndroidTranslate;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
public class AndroidTranslate extends Activity {
EditText MyInputText;
Button MyTranslateButton;
TextView MyOutputText;
Spinner spinner_InputLanguage, spinner_OutputLanguage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyInputText = (EditText)findViewById(R.id.InputText);
MyTranslateButton = (Button)findViewById(R.id.TranslateButton);
MyOutputText = (TextView)findViewById(R.id.OutputText);
MyTranslateButton.setOnClickListener(MyTranslateButtonOnClickListener);
ArrayAdapter<CharSequence> adapter
= ArrayAdapter.createFromResource(this,
R.array.language, android.R.layout.simple_spinner_item);
spinner_InputLanguage = (Spinner) findViewById(R.id.spinner_InputLanguage);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_InputLanguage.setAdapter(adapter);
spinner_OutputLanguage = (Spinner) findViewById(R.id.spinner_OutputLanguage);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_OutputLanguage.setAdapter(adapter);
}
private Button.OnClickListener MyTranslateButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String InputString;
String OutputString = null;
InputString = MyInputText.getText().toString();
Language fromLanguage =
Language.valueOf((String)spinner_InputLanguage
.getItemAtPosition((int) spinner_InputLanguage
.getSelectedItemId()));
Language toLanguage =
Language.valueOf((String)spinner_OutputLanguage
.getItemAtPosition((int) spinner_OutputLanguage
.getSelectedItemId()));
try {
Translate.setHttpReferrer("http://z4android.blogspot.com/");
OutputString = Translate.execute(InputString,
fromLanguage, toLanguage);
} catch (Exception ex) {
ex.printStackTrace();
OutputString = "Error";
}
MyOutputText.setText(OutputString);
}
};
}
AndroidTranslate.java can be downloaded here.
In this exercise, two spinner are used to select the language to be translated from and to. A array-string are used to store the languages.
It can be retrieved using:
(String)spinner_InputLanguage.getItemAtPosition((int) spinner_InputLanguage.getSelectedItemId())
(String)spinner_OutputLanguage.getItemAtPosition((int) spinner_OutputLanguage.getSelectedItemId())
It's same as the parameters to be passed to Translate.execute(), but in String type, not the expected Language type. So we have to use the Language.valueOf() to convert it to Language type.
No comments:
Post a Comment