<p>This post is about how to setup ML Kit’s language Identification feature in Android application with the help of simple demo app.</p> 
<h4><span style="color: #000080;"><strong>ML Kit&#8217;s Language Identification API</strong></span></h4> 
<p>ML Kit&#8217;s language Identification API allows you to identify the language of a string of text. You can get the string&#8217;s most likely language as well as confidence scores for all of the string&#8217;s possible languages.</p> 
 
 
 
<div class="wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex"> 
<div class="wp-block-button"><a class="wp-block-button__link has-white-color has-text-color has-background" style="background-color: #040a65;" href="https://github.com/arunk7839/MLKit-LanguageID"><strong>DOWNLOAD CODE</strong></a></div> 
</div> 
<p><amp-youtube layout="responsive" width="1200" height="675" data-videoid="lsLjQlSI62A" title="Language Identification with ML Kit on Android"><a placeholder href="https://youtu.be/lsLjQlSI62A"><img src="https://i.ytimg.com/vi/lsLjQlSI62A/hqdefault.jpg" layout="fill" object-fit="cover" alt="Language Identification with ML Kit on Android"></a></amp-youtube></p> 
<h4><span style="color: #000080;"><strong>Creating new project</strong></span></h4> 
<p>1 . Create a new project by going to File ⇒ New Android Project, select Empty Activity , provide app name, select language to kotlin and then finally click on <span style="color: #008000;"><strong>finish</strong></span>.</p> 
<h5><strong><span style="color: #000080;">Adding Dependency</span></strong></h5> 
<p>2 . Open <span style="color: #008000;"><strong>build.gradle (Module: app)</strong></span> file and add the ML Kit’s language Identification API dependency inside the <strong><span style="color: #008000;">dependencies</span></strong> section:</p> 
<p><strong><span style="color: #0000ff;">build.gradle</span></strong></p> 
<pre>dependencies {<br /><strong><span style="color: #008000;"> //bundled language identification dependency</span></strong><br /> implementation 'com.google.mlkit:language-id:17.0.1'<br />}</pre> 
<h5><span style="color: #000080;"><strong>Creating Layout File</strong></span></h5> 
<p>3. The activity_main.xml layout file defines the UI of the application.</p> 
<p><strong><span style="color: #0000ff;">activity_main.xml</span></strong></p> 
<pre><;?xml version="1.0" encoding="utf-8"?>;<br /><;androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> xmlns:app="http://schemas.android.com/apk/res-auto"<br /> xmlns:tools="http://schemas.android.com/tools"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"<br /> android:layout_margin="10dp"<br /> tools:context=".MainActivity">;<br /><br /> <;EditText<br /> android:id="@+id/edt_input"<br /> android:layout_width="match_parent"<br /> android:layout_height="wrap_content"<br /> android:hint="Enter yout text"<br /> android:inputType="text"<br /> app:layout_constraintEnd_toEndOf="parent"<br /> app:layout_constraintStart_toStartOf="parent"<br /> app:layout_constraintTop_toTopOf="parent" />;<br /><br /> <;Button<br /> android:id="@+id/btn_identify_language"<br /> android:layout_width="match_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_marginTop="30dp"<br /> android:text="Identify Language"<br /> app:layout_constraintEnd_toEndOf="@+id/tv_output_language"<br /> app:layout_constraintStart_toStartOf="@+id/tv_output_language"<br /> app:layout_constraintTop_toBottomOf="@+id/tv_output_language" />;<br /><br /> <;Button<br /> android:id="@+id/btn_possible_language"<br /> android:layout_width="match_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_marginTop="10dp"<br /> android:text="Identify Possible Language"<br /> app:layout_constraintEnd_toEndOf="@+id/btn_identify_language"<br /> app:layout_constraintStart_toStartOf="@+id/btn_identify_language"<br /> app:layout_constraintTop_toBottomOf="@+id/btn_identify_language" />;<br /><br /> <;TextView<br /> android:id="@+id/tv_input_text"<br /> android:layout_width="match_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_marginTop="30dp"<br /> android:textSize="18sp"<br /> app:layout_constraintEnd_toEndOf="@+id/edt_input"<br /> app:layout_constraintStart_toStartOf="@+id/edt_input"<br /> app:layout_constraintTop_toBottomOf="@+id/edt_input" />;<br /><br /> <;TextView<br /> android:id="@+id/tv_output_language"<br /> android:layout_width="match_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_marginTop="10dp"<br /> android:textSize="18sp"<br /> app:layout_constraintEnd_toEndOf="@+id/tv_input_text"<br /> app:layout_constraintStart_toStartOf="@+id/tv_input_text"<br /> app:layout_constraintTop_toBottomOf="@+id/tv_input_text" />;<br /><br /><;/androidx.constraintlayout.widget.ConstraintLayout>;</pre> 
<h5><span style="color: #000080;"><strong>Identify the language of a string</strong></span></h5> 
<p>4 . To identify the language of a string, create an instance of <strong><span style="color: #008000;">LanguageIdentifier</span></strong>, and then pass the string to the<span style="color: #008000;"><strong> identifyLanguage()</strong></span> method of LanguageIdentifier.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>
 
<pre><span style="color: #008000;"><span style="color: #000000;">val languageIdentifier = LanguageIdentification.getClient()</span></span><span style="color: #008000;"><br /></span><strong><span style="color: #008000;"><br />// Identify a language.</span></strong><br />private fun identifyLanguage(inputText: String) {<br /> languageIdentifier<br /> .identifyLanguage(inputText)<br /> .addOnSuccessListener { identifiedLanguage ->;<br /> tvLanguage.text = "Identified language(s): " + identifiedLanguage<br /> }<br /> .addOnFailureListener { e ->;<br /> Log.e(TAG, "Language identification error", e)<br /> }<br />}</pre> 
<ul> 
<li>If the call succeeds, <span style="color: #008000;"><strong>identified language</strong></span> is passed to the <strong><span style="color: #008000;">success</span></strong> listener or If no language is detected, <span style="color: #008000;"><strong>und (undetermined)</strong></span> is passed.</li> 
<li>In case of error, exception is passed to the <span style="color: #008000;"><strong>failure</strong></span> listener.</li> 
</ul> 
<h5><strong><span style="color: #000080;">Identify the possible languages of a string</span></strong></h5> 
<p>5 . To get the confidence values of a string&#8217;s most likely languages, get an instance of <strong><span style="color: #008000;">LanguageIdentifier</span></strong> and then pass the string to the <span style="color: #008000;"><strong>identifyPossibleLanguages()</strong></span> method.</p> 
<pre>languageIdentifier = LanguageIdentification.getClient()<br /><br /><span style="color: #008000;"><strong>// Identify all possible languages.</strong></span><br />private fun identifyPossibleLanguages(inputText: String) {<br /> languageIdentifier<br /> .identifyPossibleLanguages(inputText)<br /> .addOnSuccessListener { identifiedLanguages ->;<br /> var output = ""<br /> for (identifiedLanguage in identifiedLanguages) {<br /> output += identifiedLanguage.languageTag + " (" + identifiedLanguage.confidence + "), "<br /> }<br /> tvLanguage.text =<br /> "Identified language (s):" + output.substring(0, output.length - 2)<br /> }<br /> .addOnFailureListener { e ->;<br /> Log.e(TAG, "Language identification error", e) <br /> }<br />}</pre> 
<ul> 
<li>If the call succeeds, a list of <strong><span style="color: #008000;">IdentifiedLanguage</span></strong> objects is passed to the success listener.</li> 
<li>In case of error, exception is passed to the <span style="color: #008000;"><strong>failure</strong></span> listener.</li> 
</ul> 
<p><span style="color: #000080;"><strong> Note:</strong> </span>language confidence values indicate the confidence that the entire string is in the given language; ML Kit doesn&#8217;t identify multiple languages in a single string.</p> 
<h5><strong><span style="color: #000080;">Complete MainActivity Code</span></strong></h5> 
<p><strong><span style="color: #0000ff;">MainActivity.kt</span></strong></p> 
<pre>class MainActivity : AppCompatActivity() {<br /> private lateinit var edtInput: EditText<br /> private lateinit var tvLanguage: TextView<br /> private lateinit var tvInput: TextView<br /> private lateinit var languageIdentifier: LanguageIdentifier<br /><br /> companion object {<br /> private const val TAG = "MainActivity"<br /> }<br /><br /> override fun onCreate(savedInstanceState: Bundle?) {<br /> super.onCreate(savedInstanceState)<br /> setContentView(R.layout.activity_main)<br /><br /> languageIdentifier = LanguageIdentification.getClient()<br /> lifecycle.addObserver(languageIdentifier)<br /> edtInput = findViewById(R.id.edt_input)<br /><br /> tvInput = findViewById(R.id.tv_input_text)<br /> tvLanguage = findViewById(R.id.tv_output_language)<br /><br /> findViewById<;Button>;(R.id.btn_identify_language).setOnClickListener { _ ->;<br /> val input = edtInput.text.toString()<br /><br /> if (!input.isEmpty()) {<br /> identifyLanguage(input)<br /> }<br /> }<br /><br /> findViewById<;Button>;(R.id.btn_possible_language).setOnClickListener { _ ->;<br /> val input = edtInput.text.toString()<br /><br /> if (!input.isEmpty()) {<br /> identifyPossibleLanguages(input)<br /> }<br /> }<br /> }<br /><br /><strong><span style="color: #008000;"> // Identify a language.</span></strong><br /> private fun identifyLanguage(inputText: String) {<br /> tvLanguage.text = "Waiting…"<br /> languageIdentifier<br /> .identifyLanguage(inputText)<br /> .addOnSuccessListener { identifiedLanguage ->;<br /> tvInput.text = "Input Text: " + inputText<br /> tvLanguage.text = "Identified language(s): " + identifiedLanguage<br /> }<br /> .addOnFailureListener { e ->;<br /> Log.e(TAG, "Language identification error", e) <br /> }<br /> }<br /><br /><strong><span style="color: #008000;"> // Identify all possible languages.</span></strong><br /> private fun identifyPossibleLanguages(inputText: String) {<br /> tvLanguage.text = "Waiting…"<br /> languageIdentifier<br /> .identifyPossibleLanguages(inputText)<br /> .addOnSuccessListener { identifiedLanguages ->;<br /> tvInput.text = "Input Text: " + inputText<br /><br /> var output = ""<br /> for (identifiedLanguage in identifiedLanguages) {<br /> output += identifiedLanguage.languageTag + " (" + identifiedLanguage.confidence + "), "<br /> }<br /> tvLanguage.text =<br /> "Identified language (s):" + output.substring(0, output.length - 2)<br /> }<br /> .addOnFailureListener { e ->;<br /> Log.e(TAG, "Language identification error", e) <br /> }<br /> }<br />}</pre> 
<h5><span style="color: #000080;"><strong>Run the App</strong></span></h5> 
<p>When you run the app it will look like this:</p> 
<p><img class="alignnone wp-image-2946" src="https://c1ctech.com/wp-content/uploads/2021/11/Screenshot_20211112-203111_MLKit-LanguageID-498x1024.jpg" alt="" width="324" height="666" data-wp-editing="1" /> <img class="alignnone wp-image-2947" src="https://c1ctech.com/wp-content/uploads/2021/11/Screenshot_20211112-203329_MLKit-LanguageID-498x1024.jpg" alt="" width="325" height="669" /></p> 
<p> ;</p> 


