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 in AndroidMainfest.xml.
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 in AndroidMainfest.xml.
AndroidMainfest.xml
encoding=”utf-8″?>
schemas.android.com/apk/res/android”
package=”com.exercise.AndroidTranslate”
android:versionCode=”1″
android:versionName=”1.0″>
android:label=”@string/app_name”>
schemas.android.com/apk/res/android”
package=”com.exercise.AndroidTranslate”
android:versionCode=”1″
android:versionName=”1.0″>
android:label=”@string/app_name”>
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
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”
/>
</textview
</button
</edittext
</textview
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”
/>
</textview
</button
</edittext
</textview
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);
}
};
}
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);
}
};
}
Thanks
akm from cdacians(www.cdacians.com)
No comments:
Post a Comment