To set other locale than the one set in the device setting, just add the debug/release check:
public class LocaleUtil {
public static Context setForceLocale(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context);
} else {
return lockLocale(context);
}
}
public static Context lockLocale(Context context) {
Locale myLocale = new Locale("en-US");
Locale.setDefault(myLocale);
Resources res = context.getResources();
Configuration conf = res.getConfiguration();
conf.setLocale(myLocale);
return context.createConfigurationContext(conf);
}
private static Context updateResources(Context context) {
Locale locale = new Locale("en-US");
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
config.setLocale(locale);
return context.createConfigurationContext(config);
}
}
Inside Your Application class:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleUtil.setForceLocale(base));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleUtil.setForceLocale(this);
}
Add a ParentActivity and make all your app's activities inherit from this one
public abstract class ParentActivity extends AppCompatActivity {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleUtil.setForceLocale(base));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
LocaleUtil.setForceLocale(this);
}
}
}
And finally add this line to AndroidManifest inside all the activity tags:
android:configChanges="keyboardHidden|orientation|screenSize"