Thursday, October 8, 2009

AndroidTranslate, using Google Translate API in Android application.

Updated@2017-06-16:
Google's CLOUD TRANSLATION API now is a paid API based on usage, refer https://cloud.google.com/translate/.


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.

20 comments:

Patel Durgesh said...

Thanks for help by provide such api.

sorry, but i am having two problems on Android Emulator:

(1)My Emulator not showing Hindi.
I choose English to Hindi.
but my emulator shows rectangle symbol.

(2)when I choose English to Gujarati
output text shows error.

so what should i do.provide solution step by step for above two problems.

Erik said...

Hello Patel Durgesh,

May be because emulator doesn't support Hindi/Gujarati, I cannot select it by Menu > Settings > Language and Keyboard > Select Language.

As I know, "shows rectangle symbol" means the character cannot be displayed.

Unknown said...

I'm a beginner into Android application i realy admired this application which i tried to run but i face one problem : that i get "Error" in the translated texte field .i'm sorry, i don't speak english well .
i neeed your help friends.
thank you .

Erik said...

Hello hajar,

I don't know your case exactly. Please note that Google Translate API is a online service, so you can take caution on:
- Grant permission to access internet.
- Make sure the 3G icon of the Android Emulator is on.
- Do you pass a proxy server? some proxy server disable connection to some site.

i don't speak english well too:)

dilip said...

try {
Translate.setHttpReferrer("http://android-er.blogspot.com/");
OutputString = Translate.execute(InputString,fromLanguage, toLanguage);

} catch (Exception ex) {
ex.printStackTrace();
OutputString = "Error";
}

this Error:- Cannot make a static reference to the non-static method execute(String, Language, Language) from the type Translate

this error occure in my code so please tell me about solve this problem.

Manas Prasad Nayak said...

Hey dear dillip i got solved the above two problems solved. 1-GoogleApi.setHttpReferrer
2-Translate.DEFAULT.execute

but still could not run the app
,still showing error.......can anyone help..
Thanks in advance

Unknown said...

It is paid API

Anonymous said...

How to fix error:
The method setHttpReferrer(String) is undefined for the type Translate

akash said...

hello...can u please help me out...im a beginner..im stuck with some errors...firstly
the setcontentview() error..showing main cannot be resolved to a variable.
and Secondly..Translate isnt declared as a class..i dont know how to get over it..so please reply fast!!

Unknown said...

import com.google.api.translate.Translate;
have an error even i imported jar file.how to reduce the error here

Anonymous said...

its not working...please check errors i need the code urgent.....

jignesh sheliya said...

it giving me error in this line
OutputString = Translate.execute(InputString, Language.ENGLISH, Language.HINDI);
like "Cannot make a static reference to the non-static method execute(String, Language, Language) from the type Translate"

Bhalla Ram Dhatarwal said...

i am new in android translate google api so i am using your code for translate English to hindi but so error at text place so plz help me
and error is it giving me error in this line
OutputString = Translate.execute(InputString, Language.ENGLISH, Language.HINDI);
like "Cannot make a static reference to the non-static method execute(String, Language, Language) from the type Translate"

Unknown said...

This project is not working it's give a error message...

Unknown said...

Getting error in sethhtpreferrer showing cannot find symbol for this method please help me out

Unknown said...

Hi
it is paid API ???

Unknown said...

@Hovsep Shakaryan yes it is paid we have to pay certain amount as per characters

Anonymous said...

where to insert google translate API key ?

Unknown said...

ry {
Translate.setHttpReferrer("http://android-er.blogspot.com/");
OutputString = Translate.execute(InputString,fromLanguage, toLanguage);

} catch (Exception ex) {
ex.printStackTrace();
OutputString = "Error";
}


output error...not working

Unknown said...

Hi,
According to your latest update
(Updated@2017-06-16...), Does that mean that the existing example will not work anymore?

Thanks,
Lior.