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>
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);
}
}
Note: Download google-api-translate-java-0.95.jar from http://code.google.com/p/google-api-translate-java/downloads/list and import it as an external jar file. (Right-click project->Properties->Java Build Path-> Libraries->Add External JARS...).
<?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>
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);
}
}
Note: Download google-api-translate-java-0.95.jar from http://code.google.com/p/google-api-translate-java/downloads/list and import it as an external jar file. (Right-click project->Properties->Java Build Path-> Libraries->Add External JARS...).
It works well,, plz tell me that how we translate from English to Urdu or from Urdu to English in Android.
ReplyDeleteI am very thankful to you.
Adnan Mehmood
u bloody..how could you say that it is working???i am trying this code from last 3 days because u said "IT WORKS WELL" but it doesn't showing anything.....
Deletewell, both of you are right . google deprecated translate api in around December 11. so prior to that is was working .
DeleteThe Google Translate has Urdu as a language to be translated from another one. All you have to do is to replace translatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH); with translatedText = t.execute("Hello World", Language.ENGLISH, Language.URDU); to translate from English to Urdu. Happy coding! :)
ReplyDeleteFYI: http://translate.google.com/#en|ur|I%20love%20you!%0A
Exception in thread "main" java.lang.Exception: Google returned the following error: [400] invalid translation language pair
ReplyDeleteat com.google.api.translate.Translate.getJSONResponse(Translate.java:182)
at com.google.api.translate.Translate.execute(Translate.java:71)
You must be passing the wrong parameter there!
ReplyDeleteHi I have an exception in log cat while using Google API Translate.
ReplyDeleteHere is the error : 12-29 09:08:36.608: I/System.out(299): Error: [google-api-translate-java] Error retrieving translation.
How can i fix that problem?
@My World There could be 2 causes of this error: 1. Either the word or text to be translated is not correct AND/OR
ReplyDelete2. The language support for the desired language is not there.
Please check.
Hi Muhammad,
ReplyDeleteThank you for giving me idea about this app. I'm new to android and java when I have imported this app I'm getting "cannot instantiate the type translate":
Translate t = new Translate();
How can I fix this problem?
I have made some changes and executing but I'm getting null value on translatedText object. Please help me with this.
ReplyDeleteThanks in advance.
Its not working ...
ReplyDeleteIt returns null ...
On declaring Translate t = new Translate ();
it adds up all the methods...
Am i forgetting to add something ?
Reply soon..Thank you in advance.
Google Translate API is paid API. How can we use it for development process on trial basis.
ReplyDeletent working
ReplyDeletenot working. An empty toast appears..
ReplyDeletenot working. An empty toast appears....how can i solve this?
ReplyDeleteit's Not working..
ReplyDeleteshowing error in
Translate t = new Translate();
Error is :
"Can't instantiate the type translate"
Hi,
DeleteI am also getting the same error. Now If u predicated the answer for that, let me know.
Thanks,
Subramanian
Translate is an interface so u can not instantiate it.
DeleteThe code is not working i got the error in this
ReplyDeleteTranslate t = new Translate();
How can i fix this problem?Please give me any suggestions
hello I got this error
ReplyDeletejava.lang.Exception: [google-api-translate-java] Referrer is not set. Call setHttpReferrer().
same problem bro..if it worked halp me 2 brother
Deleteunfortunately this projects is closed.....why this?
ReplyDeletemeans forcefully closed error
ReplyDeletegoogle closed the translate apiv1 ..now we need to shift apiv2 ...check out that one
ReplyDeleteI am using api v2 but To translate api to charge but still I paid him some errors in translation or project falls.
ReplyDeleteERROR
[google-api-translate-java] error retrieving translation
does't working
ReplyDeletepackage com.example.traslater;
ReplyDeleteimport android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
String query;
TextView t1;
WebView w1;
EditText e1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText) findViewById(R.id.editText1);
Button b1 =(Button)findViewById(R.id.button1);
w1=(WebView)findViewById(R.id.webView1);
// query=e1.getText().toString();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String url = "https://translate.google.lk/?hl=en#en/si/"+e1.getText();
w1.setWebChromeClient(new MyWebViewClient());
w1.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
WebSettings webSettings = w1.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setJavaScriptEnabled(true);
if(url != null){
w1.loadUrl(url);
}
}
});
}
private class MyWebViewClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
How to get that translated text into our android application
Deleteerror at this line sir Translate t = new Translate(); tell me the sol for that
ReplyDeletehow can we import google translate api in ouer project (android studio)
ReplyDeletehello anum,
Deleteput library file on your project directory "app\lib" and
import this content in gradle file "compile files('libs/google-api-translate-java-0.95.jar')"
hello anum,
Deleteput library file on your project directory "app\lib" and
import this content in gradle file "compile files('libs/google-api-translate-java-0.95.jar')"
cuci sofa bekasi,
ReplyDeletecuci spring bed bekasi,
cuci sofa,
cuci spring bed,
cuci sofa jakarta,
cuci spring bed bekasi
i want to use this to translate to my local language but i dnt know how.........
ReplyDeleteappvn app
ReplyDelete