Use Google Translate API in Android With No Limit
How to make translator App in Android Studio
In this article, I will teach you how to use Google translate API free unlimited request in any android app. This way is totally free to develop a translation app.
Here is the final output of this example.
I have created some steps to create a Language Translator App. Here you can see these steps in the following.
- Create a new project in the android studio go to File>New project
- Go to the manifest file declare permission for the internet. Like this.
3. Go to the app level Gradle file and put this line
useLibrary 'org.apache.http.legacy'
Like this.
4. Now we have to create the translate_api class which extends AsyncTask class so that we can make network calls to the google translate API. Here is the source code.
If you want to learn more about AsyncTask the click here.
5. Create a simple design to translate the text. In this layout, I have created three EditText, On TextView, and a Button. First EditText is for text input, second is for taking the language code for text which is going to be translated, third EditText is for taking the language code in which language you want to translate. I have given the all language code list at the bottom of this article. Here is the design code.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/text"
android:hint="Enter text here.."
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/from_lang"
android:hint="Enter from language code"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/to_lang"
android:hint="Enter to language code"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="Translated Text:"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/translated_text"
android:padding="10dp"
android:textSize="25sp"
android:layout_width="match_parent"
android:minHeight="100dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnTranslate"
android:text="Translate"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
6. Here is the code for using our translate_api class in the activity class
package com.rashid.app.languagetranslation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText text,fromLangCode,toLangCode;
TextView translatedText;
Button btnTranslate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUi();
btnTranslate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
translate_api translate=new translate_api();
translate.setOnTranslationCompleteListener(new translate_api.OnTranslationCompleteListener() {
@Override
public void onStartTranslation() {
// here you can perform initial work before translated the text like displaying progress bar
}
@Override
public void onCompleted(String text) {
// "text" variable will give you the translated text
translatedText.setText(text);
}
@Override
public void onError(Exception e) {
}
});
translate.execute(text.getText().toString(),fromLangCode.getText().toString(),toLangCode.getText().toString());
}
});
}
private void initUi() {
text=findViewById(R.id.text);
fromLangCode=findViewById(R.id.from_lang);
toLangCode=findViewById(R.id.to_lang);
translatedText=findViewById(R.id.translated_text);
btnTranslate=findViewById(R.id.btnTranslate);
}
}
The complete source code of this article is available on my GitHub repository.
Click here to find the source code.
All Language ISO Code list
Abkhazian
ab
Afar
aa
Afrikaans
af
Akan
ak
Albanian
sq
Amharic
am
Arabic
ar
Aragonese
an
Armenian
hy
Assamese
as
Avaric
av
Avestan
ae
Aymara
ay
Azerbaijani
az
Bambara
bm
Bashkir
ba
Basque
eu
Belarusian
be
Bengali (Bangla)
bn
Bihari
bh
Bislama
bi
Bosnian
bs
Breton
br
Bulgarian
bg
Burmese
my
Catalan
ca
Chamorro
ch
Chechen
ce
Chichewa, Chewa, Nyanja
ny
Chinese
zh
Chinese (Simplified)
zh-Hans
Chinese (Traditional)
zh-Hant
Chuvash
cv
Cornish
kw
Corsican
co
Cree
cr
Croatian
hr
Czech
cs
Danish
da
Divehi, Dhivehi, Maldivian
dv
Dutch
nl
Dzongkha
dz
English
en
Esperanto
eo
Estonian
et
Ewe
ee
Faroese
fo
Fijian
fj
Finnish
fi
French
fr
Fula, Fulah, Pulaar, Pular
ff
Galician
gl
Gaelic (Scottish)
gd
Gaelic (Manx)
gv
Georgian
ka
German
de
Greek
el
Greenlandic
kl
Guarani
gn
Gujarati
gu
Haitian Creole
ht
Hausa
ha
Hebrew
he
Herero
hz
Hindi
hi
Hiri Motu
ho
Hungarian
hu
Icelandic
is
Ido
io
Igbo
ig
Indonesian
id, in
Interlingua
ia
Interlingue
ie
Inuktitut
iu
Inupiak
ik
Irish
ga
Italian
it
Japanese
ja
Javanese
jv
Kalaallisut, Greenlandic
kl
Kannada
kn
Kanuri
kr
Kashmiri
ks
Kazakh
kk
Khmer
km
Kikuyu
ki
Kinyarwanda (Rwanda)
rw
Kirundi
rn
Kyrgyz
ky
Komi
kv
Kongo
kg
Korean
ko
Kurdish
ku
Kwanyama
kj
Lao
lo
Latin
la
Latvian (Lettish)
lv
Limburgish ( Limburger)
li
Lingala
ln
Lithuanian
lt
Luga-Katanga
lu
Luganda, Ganda
lg
Luxembourgish
lb
Manx
gv
Macedonian
mk
Malagasy
mg
Malay
ms
Malayalam
ml
Maltese
mt
Maori
mi
Marathi
mr
Marshallese
mh
Moldavian
mo
Mongolian
mn
Nauru
na
Navajo
nv
Ndonga
ng
Northern Ndebele
nd
Nepali
ne
Norwegian
no
Norwegian bokmål
nb
Norwegian nynorsk
nn
Nuosu
ii
Occitan
oc
Ojibwe
oj
Old Church Slavonic, Old Bulgarian
cu
Oriya
or
Oromo (Afaan Oromo)
om
Ossetian
os
Pāli
pi
Pashto, Pushto
ps
Persian (Farsi)
fa
Polish
pl
Portuguese
pt
Punjabi (Eastern)
pa
Quechua
qu
Romansh
rm
Romanian
ro
Russian
ru
Sami
se
Samoan
sm
Sango
sg
Sanskrit
sa
Serbian
sr
Serbo-Croatian
sh
Sesotho
st
Setswana
tn
Shona
sn
Sichuan Yi
ii
Sindhi
sd
Sinhalese
si
Siswati
ss
Slovak
sk
Slovenian
sl
Somali
so
Southern Ndebele
nr
Spanish
es
Sundanese
su
Swahili (Kiswahili)
sw
Swati
ss
Swedish
sv
Tagalog
tl
Tahitian
ty
Tajik
tg
Tamil
ta
Tatar
tt
Telugu
te
Thai
th
Tibetan
bo
Tigrinya
ti
Tonga
to
Tsonga
ts
Turkish
tr
Turkmen
tk
Twi
tw
Uyghur
ug
Ukrainian
uk
Urdu
ur
Uzbek
uz
Venda
ve
Vietnamese
vi
Volapük
vo
Wallon
wa
Welsh
cy
Wolof
wo
Western Frisian
fy
Xhosa
xh
Yiddish
yi, ji
Yoruba
yo
Zhuang, Chuang
za
Zulu
zu