Essential Android Localization with resources edit forum

Change locale of android application programatically

In above examples you understand how to localize resources of application. Following example explain how to change the application locale within application, not from device. In order to change Application locale only, you can use below locale util.

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.view.ContextThemeWrapper;

import java.util.Locale;

 * Created by Umesh on 10/10/16.
public class LocaleUtils {

    private static Locale mLocale;

    public static void setLocale(Locale locale){
        mLocale = locale;
        if(mLocale != null){

    public static void updateConfiguration(ContextThemeWrapper wrapper){
        if(mLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            Configuration configuration = new Configuration();

    public static void updateConfiguration(Application application, Configuration configuration){
        if(mLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1){
            Configuration config = new Configuration(configuration);
            config.locale = mLocale;
            Resources res = application.getBaseContext().getResources();
            res.updateConfiguration(configuration, res.getDisplayMetrics());

    public static void updateConfiguration(Context context, String language, String country){
        Locale locale = new Locale(language,country);
        if(mLocale != null){
            Resources res = context.getResources();
            Configuration configuration = res.getConfiguration();
            configuration.locale = mLocale;

    public static String getPrefLangCode(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getString("lang_code","en");

    public static void setPrefLangCode(Context context, String mPrefLangCode) {

        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();

    public static  String getPrefCountryCode(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getString("country_code","US");

    public static void setPrefCountryCode(Context context,String mPrefCountryCode) {

        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();

Initialize locale that user preferred, from Application class.

public class LocaleApp extends Application{

    public void onCreate() {

        LocaleUtils.setLocale(new Locale(LocaleUtils.getPrefLangCode(this), LocaleUtils.getPrefCountryCode(this)));
        LocaleUtils.updateConfiguration(this, getResources().getConfiguration());

You also need to create a base activity and extend this activity to all other activity so that you can change locale of application only one place as follows :

public abstract class LocalizationActivity extends AppCompatActivity {

    public LocalizationActivity() {

    // We only override onCreate
    protected void onCreate(@Nullable Bundle savedInstanceState) {

Note : Always initialize locale in constructor.

Now you can use LocalizationActivity as follow.

public class MainActivity extends LocalizationActivity {

    protected void onCreate(Bundle savedInstanceState) {

Note: When you change locale of application programmatically, need to restart your activity to take the effect of locale change In order to work properly for this solution you and use locale from shared preferences on app startup you android:name=".LocaleApp" in you Manifest.xml.

Sometimes Lint checker prompt to create the release build. To solve such issue follow below options.


If you want to disable translation for some strings only then add following attribute to default string.xml

<string name="developer" translatable="false">Developer Name</string>


Ignore all missing translation from resource file add following attribute It’s the ignore attribute of the tools namespace in your strings file, as follows:

<?xml version="1.0" encoding="utf-8"?>
  tools:ignore="MissingTranslation" >
  <!-- your strings here; no need now for the translatable attribute -->



Another way to disable non-translatable string

If you have a lot of resources that should not be translated, you can place them in a file named donottranslate.xml and lint will consider all of them non-translatable resources.


You can also add locale in resource file

    tools:locale="en" tools:ignore="MissingTranslation">

You can also disable missing translation check for lint from app/build.gradle

lintOptions {
        disable 'MissingTranslation'

Table Of Contents
64 Menu
84 Localization with resources
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close