Pages

Thursday, June 2, 2011

AndroidTranslate, using Google Translate API in Android application.

It's a simple Android Application using google-api-translate-java to involve Google Translate, to translate the text input in the TextView in English to French.



In order to use google-api-translate-java in our application, we have to do some preparation as described in the article google-api-translate-java.

In our project, we have to grand permission to access internet, add the code <uses-permission android:name="android.permission.INTERNET" /> in AndroidMainfest.xml.

AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.exercise.AndroidTranslate"
   android:versionCode="1"
   android:versionName="1.0">
 <application android:icon="@drawable/icon" android:label="@string/app_name">
     <activity android:name=".AndroidTranslate"
               android:label="@string/app_name">
         <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
     </activity>
 </application>
 <uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>


Modify main.xml to include a EditText to input text to be translate, a Button to start translation, and a TextView to display the result.

main.xml
<?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"
/>
<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.

Finally, modify AndroidTranslate.java to implement the function.

AndroidTranslate.java
package com.exercise.AndroidTranslate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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;

 /** 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);
 }

 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();

  try {
   Translate.setHttpReferrer("http://android-er.blogspot.com/");
   OutputString = Translate.execute(InputString,
     Language.ENGLISH, Language.FRENCH);
  } catch (Exception ex) {
   ex.printStackTrace();
   OutputString = "Error";
     }

  MyOutputText.setText(OutputString);
  
 }
 
 };

}

AndroidTranslate.java can be downloaded here.

1 comment:

  1. The tendency to use mobile applications are increasingly popular these days. The development of Android Application Development, you can make a very advanced mobile applications. Android is an open source platform for developing mobile applications.

    ReplyDelete

Popular Posts