How to Create Language Translator App Using Android Studio & Java Programming language Android App Development series part 1.
How to create an app a translator like to translate languages.
- Create Language Translator App Using Android Studio, Android App Development.
- Using JAVA Language and android Studio.
- Make an android app that can translate any language to another specific language.
Ajju Translator App: is just an android app. we created this during teaching on YouTube. So here is source code to create any of language Translator App for your project. Make sure read this Carefully and get source code line by line. Don’t miss any semi colon or a character you may get errors.
I have developed this app which can translate English to Urdu this app source code is available you to use and make your app.
Make sure to support us thank you!
Step 1: Open Android studio and paste this code into your java file.
Main Activity java code.
Here are these Libraries you must need to Import:
MainActivity.java
package com.ajjuvirus.ajjutranslator;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions;
import com.google.firebase.ml.naturallanguage.FirebaseNaturalLanguage;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateLanguage;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslator;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslatorOptions;
import java.util.ArrayList;
import java.util.Locale;
After Importing the Libraries next step is to: Paste this code into your MainActivity.class file.
public class MainActivity extends AppCompatActivity {
private Spinner fromSpinner, toSpinner;
private TextInputEditText sourceEdt;
private ImageView micIV;
private MaterialButton translatBtn;
private TextView translatedTV;
String[] fromLanguage = {"from","English","Afrikaans","Arabic","Belorussian","Bulgarian","Bengali","Catalan","Czech"
,"Welsh","Hindi","Urdu"};
String[] toLanguage = {"from","English","Afrikaans","Arabic","Belorussian","Bulgarian","Bengali","Catalan","Czech"
,"Welsh","Hindi","Urdu"};
private static final int REQUEST_PERMISSION_CODE =1;
int languageCode,fromLanguageCode, toLanguageCode =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fromSpinner = findViewById(R.id.idFromSpinner);
toSpinner = findViewById(R.id.idToSpinner);
micIV = findViewById(R.id.idIVMic);
sourceEdt = findViewById(R.id.idEdtSource);
translatBtn = findViewById(R.id.idBtnTranslate);
translatedTV = findViewById(R.id.idTVTranslatedTV);
fromSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
fromLanguageCode = getLanguageCode(fromLanguage[position]);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
ArrayAdapter fromAdapter =new ArrayAdapter(this,R.layout.spinner_item,fromLanguage);
fromAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fromSpinner.setAdapter(fromAdapter);
toSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
toLanguageCode = getLanguageCode(toLanguage[position]);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
ArrayAdapter toAdapter = new ArrayAdapter(this,R.layout.spinner_item,toLanguage);
toAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
toSpinner.setAdapter(toAdapter);
translatBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
translatedTV.setText("");
if (sourceEdt.getText().toString().isEmpty()) {
//remove // and white spaces after pasting
Toast.//makeText //(Main Activity. this, "Now enter any text to translate", //Toast.LENGTH_SHORT ).show();
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_PERMISSION_CODE){
if(resultCode == RESULT_OK && data != null){
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
sourceEdt.setText(result.get(0));
}
}
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this,"Failed to download language model"+e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
//after pasting remove // from this below line.
//public void translate//Text(int from//languageCode, int //tolanguageCode, //String source)
{
translatedTV.setText("Downloading model..");
FirebaseTranslatorOptions options = new FirebaseTranslatorOptions.Builder()
.setSourceLanguage(fromlanguageCode).setTargetLanguage(tolanguageCode)
.build();
FirebaseTranslator translator = FirebaseNaturalLanguage.getInstance().getTranslator(options);
FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder().build();
translator.downloadModelIfNeeded(conditions).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
translatedTV.setText("Translating..");
translator.translate(source).addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
translatedTV.setText(s);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this,"Fail to translate"+e.getMessage(),Toast.LENGTH_SHORT).show();
}
//these are the language code you can change it easily….
// String[] fromLanguage = {"from","English","Afrikaans","Arabic","Belorussian","Bulgarian","Bengali","Catalan","Czech"
// ,"Welsh","Hindi","Urdu",};
public int getLanguageCode(String language){
int languageCode = 0;
switch (language) {
case "English":
languageCode = FirebaseTranslateLanguage.EN;
break;
case "Afrikaans":
languageCode = FirebaseTranslateLanguage.AF;
break;
case "Arabic":
languageCode = FirebaseTranslateLanguage.AR;
break;
case "Belorussian":
languageCode = FirebaseTranslateLanguage.BE;
break;
case "Bulgarian":
languageCode = FirebaseTranslateLanguage.BG;
break;
case "Bengali":
languageCode = FirebaseTranslateLanguage.BN;
break;
case "Catalan":
languageCode = FirebaseTranslateLanguage.CA;
break;
case "Czech":
languageCode = FirebaseTranslateLanguage.CS;
break;
case "Welsh":
languageCode = FirebaseTranslateLanguage.CY;
break;
case "Hindi":
languageCode = FirebaseTranslateLanguage.HI;
break;
case "Urdu":
languageCode = FirebaseTranslateLanguage.UR;
break;
default:
languageCode = 0;
}
return languageCode;
}
}
Step 2: Open your Resources and then layout files. Paste this code into you main layout file.
Layout File XML Code.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ajju_Translator"
android:textAlignment="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="@color/yellow"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:padding="40dp"
/>
<Spinner
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/idFromSpinner"
android:layout_margin="3dp"
android:padding="3dp"
/>
<ImageView
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_arrow_right"
app:tint="@color/white"
android:layout_gravity="center"
/>
<Spinner
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:id="@+id/idToSpinner"
android:layout_weight="1"
android:gravity="center"
android:padding="3dp"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:hint="Source Text"
android:padding="5dp"
android:textColorHint="@color/white"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/idEdtSource"
android:ems="10"
android:importantForAutofill="no"
android:inputType="textImeMultiLine"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="14sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OR"
android:textAllCaps="true"
android:textColor="@color/white"
android:textSize="20sp"
android:textAlignment="center"
android:gravity="center"
android:layout_marginTop="5dp"
/>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:src="@drawable/ic_mic"
app:tint="@color/yellow"
android:id="@+id/idIVMic"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Say Something"
android:textAlignment="center"
android:gravity="center"
android:textColor="@color/white"
android:layout_gravity="center"
android:textSize="15sp"
/>
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/idBtnTranslate"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:text="Translate"
android:textAllCaps="false"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/idTVTranslatedTV"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Translated Text"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/yellow"
android:textSize="18sp"
android:gravity="center"
/>
Step 3: Open your Main layout files. Paste this code into your new layout file.
Spinner Code XML.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"<br>android:layout_width="match_parent"<br>android:layout_height="wrap_content"<br>android:gravity="center"<br>android:padding="4dp"<br>android:textAlignment="center"<br>android:textColor="@color/white">
Run & Debug your app now hopefully it should run without any error this source code is test by Sain Azeem Tech.
Hope you love it. Please Make sure to Support us!
Thank you keep learning.
For video lecture Subscribe Now : Sain Azeem YouTube!
Leave a Reply