Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
How to Change Password of User in Android using Firebase?
Next article icon

How to Create Language Translator in Android using Firebase ML Kit?

Last Updated : 25 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In the previous article, we have seen using Language detector in Android using Firebase ML kit. In this article, we will take a look at the implementation of Language translator in Android using Firebase ML Kit in Android. 

What we are going to build in this article? 

We will be building a simple application in which we will be showing an EditText field and we will add any input to that TextField. Along with that, we will be displaying a Button to translate that text to the Hindi language. After clicking that button our text will be translated which we can get to see in the text view.

Steps to Implement Language Translator in Android

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Connect your app to Firebase

After creating a new project in Android Studio connect your app to Firebase. For connecting your app to firebase. Navigate to Tools on the top bar. After that click on Firebase. A new window will open on the right side. Inside that window click on Firebase ML and then click on Use Firebase ML kit in Android. You can see the option in the below screenshot.  

firebase-ml-kit

After clicking on this option you will get to see the below screen. On this screen click on Connect to Firebase option to connect your app to Firebase. Click on Connect option to connect your app to Firebase and add the below dependency to your build.gradle.kts file.  


Step 3: Adding dependency for language translation to build.gradle.kts file

Navigate to the Gradle Scripts > build.gradle.kts(Module:app) and add the below dependency in the dependencies section.   

dependencies {
...
implementation ("com.google.firebase:firebase-core:21.1.1")
implementation ("com.google.firebase:firebase-ml-natural-language:22.0.1")
implementation ("com.google.firebase:firebase-ml-natural-language-translate-model:20.0.9")
}


Step 4: Adding permissions to access the Internet in your Android App

Navigate to the app > manifests > AndroidManifest.xml file and add the below code to it. Comments are added in the code to get to know in more detail.  

<uses-permission android:name="android.permission.INTERNET"/>

Step 5: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

activity_main.xml:

XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:gravity="center"     android:orientation="vertical"     tools:context=".MainActivity">      <EditText         android:id="@+id/editText"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margin="64dp"         android:autofillHints="none"         android:hint="Enter text to translate"         android:inputType="none"         android:textColor="@color/black"         android:textSize="20sp" />      <Button         android:id="@+id/button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Translate" />      <TextView         android:id="@+id/textView"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margin="25dp"         android:text="Translated language"         android:textAlignment="center"         android:textSize="20sp" />  </LinearLayout> 

Design UI:

translate-design-ui


Step 6: Working with the MainActivity file

Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.

MainActivity File:

MainActivity.java
package org.geeksforgeeks.demo;  import android.os.Bundle; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; 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;  public class MainActivity extends AppCompatActivity {      private boolean isDownloaded = false;     private EditText editText;     private TextView textView;     private Button button;          // Firebase Language Translator     private FirebaseTranslator translator;       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Firebase Translator Options         FirebaseTranslatorOptions options = new FirebaseTranslatorOptions.Builder()                 .setSourceLanguage(FirebaseTranslateLanguage.EN) // Source language: English                 .setTargetLanguage(FirebaseTranslateLanguage.FR) // Target language: French                 .build();          // Get Firebase Translator Instance         translator = FirebaseNaturalLanguage.getInstance().getTranslator(options);          editText = findViewById(R.id.editText);         textView = findViewById(R.id.textView);         button = findViewById(R.id.button);          // Translator Button Click Listener         button.setOnClickListener(view -> {             String inputText = editText.getText().toString();             if (!isDownloaded) {                 downloadModel(inputText);             } else {                 translateText(inputText);             }         });     }      // Function to Download Target Language Model     private void downloadModel(String inputText) {         FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder()                 .requireWifi()                 .build();          translator.downloadModelIfNeeded(conditions)                 .addOnSuccessListener(unused -> {                     Toast.makeText(MainActivity.this,                              "Please wait, language model is downloading.",                              Toast.LENGTH_SHORT).show();                     isDownloaded = true;                                          // Translate after download                     translateText(inputText);                  })                 .addOnFailureListener(e -> {                     Toast.makeText(MainActivity.this, "Download failed", Toast.LENGTH_SHORT).show();                 });     }      // Function to Translate User Input     // and Display in TextView     private void translateText(String inputText) {         translator.translate(inputText)                 .addOnSuccessListener(translatedText -> textView.setText(translatedText))                 .addOnFailureListener(e ->                      Toast.makeText(MainActivity.this, "Failed to translate", Toast.LENGTH_SHORT).show());     } } 
MainActivity.kt
@file:Suppress("DEPRECATION")  package org.geeksforgeeks.demo  import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity 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   class MainActivity : AppCompatActivity() {     private var isDownloaded = false     private lateinit var editText: EditText     private lateinit var textView: TextView     private lateinit var button: Button      // firebase language translator     private lateinit var translator: FirebaseTranslator      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // creating firebase translate option.         val options =             FirebaseTranslatorOptions.Builder()                 // setting current language to english                 .setSourceLanguage(FirebaseTranslateLanguage.EN)                                  // setting target language to french                 .setTargetLanguage(FirebaseTranslateLanguage.FR)                 .build()          // get instance for firebase natural language translator         translator = FirebaseNaturalLanguage.getInstance().getTranslator(options)          editText = findViewById(R.id.editText)         textView = findViewById(R.id.textView)         button = findViewById(R.id.button)          // translator button         button.setOnClickListener {             val string = editText.getText().toString()             if(!isDownloaded) {                              // this downloads the target language modal                 downloadModal(string)             } else {                              // directly translates if modal is already downloaded                 translateLanguage(string)             }         }     }      // function to download target language modal     private fun downloadModal(input: String) {         val conditions = FirebaseModelDownloadConditions.Builder().requireWifi().build()          translator.downloadModelIfNeeded(conditions)             .addOnSuccessListener {                              // downloaded successfully                 Toast.makeText(                     this@MainActivity,                     "Please wait language modal is being downloaded.",                     Toast.LENGTH_SHORT                 ).show()                                  // translates the user input to target language                 translateLanguage(input)                 isDownloaded = true             }.addOnFailureListener {                              // download failed                 Toast.makeText(this@MainActivity, "Download failed", Toast.LENGTH_SHORT).show()             }     }      // function to translate user input to     // target language and display in textview     private fun translateLanguage(input: String) {         // translates         translator.translate(input)             .addOnSuccessListener { translatedText ->                                  // displaying in textview                 textView.text = translatedText             }             .addOnFailureListener {                 Toast.makeText(this@MainActivity, "Fail to translate", Toast.LENGTH_SHORT).show()             }     } } 

Output:

Important: When you are using the app for the first time. It will take some time because it will download the modal in the background. 

Note: We are not adding multiple language support in this application because for each language we have to download the language conversion model so it will make the app heavier and language translation will take so much time.  

Refer to the following github repo for the entire code: Language_Translator_Android_Firebase


Next Article
How to Change Password of User in Android using Firebase?

C

chaitanyamunje
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Machine Learning
  • Android
  • AI-ML-DS
  • Technical Scripter 2020
  • Kotlin Android
  • Android Projects
  • Java-Android
Practice Tags :
  • Java
  • Machine Learning

Similar Reads

  • How to Label Image in Android using Firebase ML Kit?
    We have seen many apps in Android in which we will detect the object present in the image whether it may be any object. In this article, we will take a look at the implementation of image labeling in Android using Firebase ML Kit.  What we are going to build in this article?  We will be building a s
    9 min read
  • Text Detector in Android using Firebase ML Kit
    Nowadays many apps using Machine Learning inside their apps to make most of the tasks easier. We have seen many apps that detect text from any image. This image may include number plates, images, and many more. In this article, we will take a look at the implementation of Text Detector in Android us
    6 min read
  • How to Change Password of User in Android using Firebase?
    In many apps, we got a feature to login using our email and password. Sometimes it happens that we forget the password and most of the time there reset our password. Here we are going to implement the same feature to Reset our password using Firebase Authentication. You may refer to the following ar
    3 min read
  • How to Create Dynamic Intro Slider in Android using Firebase Firestore?
    We have seen creating a basic Intro Slider in Android which is used to inform our users regarding the features of our app and many more. In this article, we will take a look at the implementation of dynamic Intro Slider in our app with the help of Firebase. With the help of Firebase Firestore, we ca
    11 min read
  • How to Push Notification in Android using Firebase In-App Messaging?
    We have seen using Firebase push notifications in Android which is used to send push notifications to our users when our user is online. These notifications are being displayed inside the notifications tab in the top section of our phone. In this article, we will take a look at the implementation of
    6 min read
  • How to Create a Medicine Tracker Android App with Firebase?
    A medicine tracker app can be a useful tool for individuals who need to take multiple medications on a regular basis. It can help users track when they need to take their medications and provide alerts and reminders to ensure they don't miss a dose. This article will look at how to build a medicine
    8 min read
  • How to Use Firebase ML Kit Smart Replies in Android?
    We have seen using chatbots in Android for replying to the most common questions from the users. In this article, we will take a look at the implementation of Firebase ML Kit smart replies in Android. Firebase ML Kit smart replies are used to provide smart replies to the questions asked by the users
    7 min read
  • How to create a Face Detection Android App using Machine Learning KIT on Firebase
    Pre-requisites: Firebase Machine Learning kitAdding Firebase to Android AppFirebase ML KIT aims to make machine learning more accessible, by providing a range of pre-trained models that can use in the iOS and Android apps. Let's use ML Kit’s Face Detection API which will identify faces in photos. By
    9 min read
  • How to Create Dynamic ListView in Android using Firebase Firestore?
    ListView is one of the most used UI components in Android which you can find across various apps. So we have seen listview in many different apps. In the previous article, we have seen implementing ListView in Android using Firebase Realtime Database. Now in this article, we will take a look at the
    9 min read
  • How to Push Notification in Android using Firebase Cloud Messaging?
    Firebase Cloud Messaging is a real-time solution for sending notifications to client apps without any kind of charges. FCM can reliably transfer notifications of up to 4Kb of payload. In this article, a sample app showing how this service can be availed is developed. Though FCM also allows sending o
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences