How to Get the current language in Android device?


While doing internalization in android application, we should know what is the current language in android device. This example demonstrate about how to Get the current language in Android device.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.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"
   tools:context=".MainActivity"
   android:background="#dde4dd"
   android:gravity="center"
   android:orientation="vertical">
   <TextView
      android:id="@+id/language"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
   <Button
      android:id="@+id/click"
      android:layout_marginTop="10dp"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="click"/>
</LinearLayout>

In the above code we have taken Button. When user click on button, it will take device country name and language and append to text view.

Step 3 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
   TextView language;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      language = findViewById(R.id.language);
      Button button = findViewById(R.id.click);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            String languagename = Locale.getDefault().getDisplayLanguage();
            String country = Locale.getDefault().getCountry();
            language.setText("Language " + languagename + " Country name " + country);
         }
      });
   }
}

In the above code, we have used Locale class to get Display language and country as shown below -

String languagename = Locale.getDefault().getDisplayLanguage();
String country = Locale.getDefault().getCountry();

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Runicon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

In the above result it shows initial screen when user click on button it will show country name and language as shown below -

Click here to download the project code

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements