david ortiz steroids oral steroids what do steroids do to your body moon face steroids liver steroids steroids for bronchitis types of steroids

Android Locale – Get Locale Name And Country Code

Last Updated on April 21, 2018

Hi and welcome to another tutorial from Codingdemos, today you will learn about Android locale API and how you can use it inside your app to get default locale and country code.

Android Get Default Locale And Country Code

By the end of this tutorial, you will have an app that looks like this. (Large preview)

In this tutorial we will be using the following:

    – Android studio version 3.0.1
    – Android emulator Nexus 5X with API 26
    – Minimum SDK API 16

1- Open up Android Studio and open any project that you have in your computer.

android studio welcome screen

Create new Android Studio project or open existing project. (Large preview)

2- Open up activity_main.xml file, inside this file you will add 2 Android TextView to show language name and code and make sure to set the text size for these 2 TextView to 30sp.


< TextView
    android:id="@+id/tvLanguage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp" />

  < TextView
    android:id="@+id/tvLanguageCode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp" />

3- The full code for activity_main.xml file will look like this:


< ?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:orientation="vertical"
  tools:context="com.codingdemos.codingdemos.MainActivity">

  < TextView
    android:id="@+id/tvLanguage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp" />

  < TextView
    android:id="@+id/tvLanguageCode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp" />
< /LinearLayout>

4- Next open up MainActivity.java file, here you need to reference those 2 Android TextView which you have added them inside activity_main.xml file.


TextView languageName = findViewById(R.id.tvLanguage);
TextView languageCode = findViewById(R.id.tvLanguageCode);

5- Now you will get to work with Android Locale API, add the following code below Android TextView code.


Locale locale = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0);

Here you use ConfigurationCompat which is a helper class to be able to have access to the configuration in backward compatibility, then you get the list of Android locale by using getLocales and finally you will use Resources to be able to access the system configuration of the Android device.

6- After initializing Locale, you will now be able to get the language name and code from Android device. Add the following code below Locale initialization.


languageName.setText(locale.getDisplayName());

Here you have initialized languageName textview with Locale properties.

You will use locale.getDisplayName() to get the language name that is currently being used in your Android device.

7- Build and run the app to see the output.

android locale language name

Using Android Locale to get language name. (Large preview)

8- Now initialize languageCode with Android Locale properties, so add the following code below languageName.


languageCode.setText(locale.getLanguage());

Here you use locale.getLanguage() to get the Android Locale country code of the language that is currently being used in your Android device.

9- Build and run the app to see the progress.

Android Get Default Locale And Country Code

Getting Android Locale country code. (Large preview)

10- When you change the language of your Android device it will apply to the app as well, for example if you change your device language to Spanish you will get the following output.

android locale spanish

Android Locale Spanish. (Large preview)

android locale hindi

Android Locale Hindi. (Large preview)

11- The full code for MainActivity.java file will look like this.


public class MainActivity extends AppCompatActivity {

    TextView languageName;
    TextView languageCode;
    Locale locale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        languageName = findViewById(R.id.tvLanguage);
        languageCode = findViewById(R.id.tvLanguageCode);
        locale = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0);

        languageName.setText(locale.getDisplayName());
        languageCode.setText(locale.getLanguage());
    }
}

12- I hope you find this tutorial helpful and if you have any question please post them in the comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>