Step 1: add library.

					dependencies {
      implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

Step 2: add below class in your project.

					package com.example.language.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;

import java.util.Locale;

public class LocaleHelper {
    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

     * the method is used to set the language at runtime
    public static Context setLocale(Context context, String language) {

        if (!LocaleHelper.getLanguage(context).equalsIgnoreCase(language)) {
            persist(context, language);

        /**updating the language for devices above android nougat **/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language);
        /** for devices having lower version of android os **/
        return updateResourcesLegacy(context, language);

    protected static void persist(Context context, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(SELECTED_LANGUAGE, language);

     * get Language Code which you selected even if return your system language
    public static String getLanguage(Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(SELECTED_LANGUAGE, Locale.getDefault().getLanguage().toLowerCase());

     * the method is used update the language of application by creating
     * object of inbuilt Locale class and passing language argument to it
    protected static Context updateResources(Context context, String language) {

        Resources res = context.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.setLocale(new Locale(language.toLowerCase())); // API 17+ only.
        res.updateConfiguration(conf, dm);
        return context.createConfigurationContext(conf);

    protected static Context updateResourcesLegacy(Context context, String language) {
        Locale locale = new Locale(language);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());

        return context;

Step 3: implements below line in your application class.

					Application.ActivityLifecycleCallbacks, LifecycleObserver

Step 4: add below line in onCreate method in Application Class.


Step 5: add this line your activity.

					LocaleHelper.setLocale(CONTEXT, LocaleHelper.getLanguage(CONTEXT));

//here LocaleHelper.getLanguage(CONTEXT) is return default language of device.
//if you want change the language in to ENGLISH then write this line..

LocaleHelper.setLocale(CONTEXT, "en");

Leave a Reply

Your email address will not be published.