How to Create Language Detector in Android using Firebase ML Kit?
Last Updated :
22 Dec, 2021
We have seen many apps providing different language supports inside their application and we also have seen many ML apps in which we will get to see that we can detect the language of the text which is entered by the user. In this article, we will create an application in which we will detect the language of the entered text in our Android App. So for detecting the language of this text. We will be using the Firebase ML Kit which will detect the language of the text and will display appropriate language code.
What we are going to build in this article?
We will be building a simple application in which we will display an EditText field and inside that, we have to add the text in any language and a button for detecting the language of that text. After adding the text we will click on detect language button the language of our text is detected and we will get to see the code for our language. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
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. Note that select Java as the programming language.
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 below screenshot.
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. You will get to see the below screen.
Click on Connect option to connect your app to Firebase and add the below dependency to your build.gradle file.
Step 3: Adding dependency for language detection to build.gradle file
Navigate to the app > Gradle Scripts > build.gradle file and add the below code to it. Comments are added in the code to get to know in more detail.
// dependency for firebase core.
implementation'com.google.firebase:firebase-core:15.0.2'
// below two dependency are used for language detection
implementation 'com.google.firebase:firebase-ml-natural-language:22.0.0'
implementation 'com.google.firebase:firebase-ml-natural-language-language-id-model:20.0.7'
Step 4: Adding Internet permissions to access the Internet in your Android App
Navigate to the app > AndroidManifest.xml file and add the below code to it. Comments are added in the code to get to know in more detail.
XML
<!--permission for internet-->
<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.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<!--edit text to enter your input-->
<EditText
android:id="@+id/idEdtLanguage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:hint="Enter your name"
android:padding="4dp"
android:textColor="@color/black"
android:textSize="20sp" />
<!--button to detect language of the input text-->
<Button
android:id="@+id/idBtnDetectLanguage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/idTVDetectedLanguageCode"
android:layout_centerInParent="true"
android:text="Detect language" />
<!--text view to display the code of entered text-->
<TextView
android:id="@+id/idTVDetectedLanguageCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtLanguage"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:gravity="center_horizontal"
android:text="Language code"
android:textAlignment="center"
android:textSize="20sp" />
</RelativeLayout>
Step 6: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;
import android.view.View;
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.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.ml.naturallanguage.FirebaseNaturalLanguage;
import com.google.firebase.ml.naturallanguage.languageid.FirebaseLanguageIdentification;
public class MainActivity extends AppCompatActivity {
// creating variables for our image view,
// text view and two buttons.
private EditText edtLanguage;
private TextView languageCodeTV;
private Button detectLanguageBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing our variables.
edtLanguage = findViewById(R.id.idEdtLanguage);
languageCodeTV = findViewById(R.id.idTVDetectedLanguageCode);
detectLanguageBtn = findViewById(R.id.idBtnDetectLanguage);
// adding on click listener for button
detectLanguageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// getting string from our edit text.
String edt_string = edtLanguage.getText().toString();
// calling method to detect language.
detectLanguage(edt_string);
}
});
}
private void detectLanguage(String string) {
// initializing our firebase language detection.
FirebaseLanguageIdentification languageIdentifier = FirebaseNaturalLanguage.getInstance().getLanguageIdentification();
// adding method to detect language using identify language method.
languageIdentifier.identifyLanguage(string).addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
// below line we are setting our
// language code to our text view.
languageCodeTV.setText(s);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// handling error method and displaying a toast message.
Toast.makeText(MainActivity.this, "Fail to detect language : \n" + e, Toast.LENGTH_SHORT).show();
}
});
}
}
Now run your app and see the output of the app:
Output:
Similar Reads
How to Create Language Translator in Android using Firebase ML Kit?
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 appl
5 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 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
8 min read
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
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 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 use Firebase UI Authentication Library in Android?
Firebase UI is a library provided by Firebase for Android apps which makes or so many tasks easy while integrating Firebase in Android. This library provides so many extra features that we can integrate into our Android very easily. In this article, we will take a look at using this library for addi
10 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 Dynamic WebView in Android with Firebase?
Converting a website into an application seems like a basic task to do on Android. With the help of WebView, we can show any webpage in our Android Application. We just have to implement the widget of WebView and add the URL inside the WebView which we have to load. So if you are looking for loading
6 min read
Google Signing using Firebase Authentication in Android
Firebase is a mobile and web application development platform. It provides services that a web application or mobile application might require. Firebase provides email and password authentication without any overhead of building the backend for user authentication. Google Sign-In is a secure way to
8 min read