In this tutorial we are going to know how we can use google translate API in Android
At first download google-api-translate-java-0.95.jar here
Import that JAR file to android buid Path (Right-click project->Properties->Java Build Path-> Libraries->Add External JARS.)
The xml file
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"
/>
</LinearLayout>
The JAVA file
Translator.java:
package com.myapps.translate;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
public class Translator extends Activity {
/** Called when the activity is first created. */
String translatedText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
Translate t = new Translate();
try {
translatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
//tv.setText(translatedText);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(this, translatedText, Toast.LENGTH_SHORT).show();
tv.setText(translatedText);
setContentView(tv);
}
}
At first download google-api-translate-java-0.95.jar here
Import that JAR file to android buid Path (Right-click project->Properties->Java Build Path-> Libraries->Add External JARS.)
The xml file
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"
/>
</LinearLayout>
The JAVA file
Translator.java:
package com.myapps.translate;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
public class Translator extends Activity {
/** Called when the activity is first created. */
String translatedText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
Translate t = new Translate();
try {
translatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
//tv.setText(translatedText);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(this, translatedText, Toast.LENGTH_SHORT).show();
tv.setText(translatedText);
setContentView(tv);
}
}
Translate t = new Translate(); - something wrong here
ReplyDeleteWhat do you mean about Translate t = new Translate().Other package ?
ReplyDeleteTranslate t = new Translate(); is not working
ReplyDeleteTranslate t = new Translate();
ReplyDeletewhich errors Cannot instantiate the type Translate
Translate is an interface, So u can't instantiate it. but you can override its methods as following :-
ReplyDeleteTranslate t =new Translate() {
@Override
public String[] execute(String[] arg0, Language[] arg1, Language[] arg2)
throws GoogleAPIException {
// TODO Auto-generated method stub
return null;
}
@Override
public String[] execute(String arg0, Language arg1, Language[] arg2)
throws GoogleAPIException {
// TODO Auto-generated method stub
return null;
}
@Override
public String[] execute(String[] arg0, Language arg1, Language arg2)
throws GoogleAPIException {
// TODO Auto-generated method stub
return null;
}
@Override
public String execute(String arg0, Language arg1, Language arg2)
throws GoogleAPIException {
// TODO Auto-generated method stub
return null;
}
};
try {
translatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
System.out.println("translatedText = ["+translatedText+"]");
It is incredible feature which you share thank for the sharing.
ReplyDeleteAndroid Development
it is not working always returns null even i changed translatev1,translatev2
ReplyDelete:-((
ReplyDeleteYou need to have a key
ReplyDeletetranslatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
ReplyDeleteEvery time Exception generated at that line
translatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
ReplyDeletei am always getting null values for the above line..
This comment has been removed by the author.
ReplyDeleteWill this jar work without internet?
ReplyDeleteThis guy doesnt know Android
ReplyDelete