Friday, May 18, 2012

Using Google Translate API In Android

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

14 comments:

  1. Translate t = new Translate(); - something wrong here

    ReplyDelete
  2. What do you mean about Translate t = new Translate().Other package ?

    ReplyDelete
  3. Translate t = new Translate(); is not working

    ReplyDelete
  4. Translate t = new Translate();
    which errors Cannot instantiate the type Translate

    ReplyDelete
  5. Translate is an interface, So u can't instantiate it. but you can override its methods as following :-
    Translate 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+"]");

    ReplyDelete
  6. It is incredible feature which you share thank for the sharing.

    Android Development

    ReplyDelete
  7. it is not working always returns null even i changed translatev1,translatev2

    ReplyDelete
  8. translatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);

    Every time Exception generated at that line

    ReplyDelete
  9. translatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
    i am always getting null values for the above line..

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Will this jar work without internet?

    ReplyDelete