Changing Locale Within the App Itself

Changing Locale within the app itself

After a good night of sleep, I found the answer on the Web (a simple Google search on the following line "getBaseContext().getResources().updateConfiguration(mConfig, getBaseContext().getResources().getDisplayMetrics());"), here it is :

link text
=> this link also shows screenshots of what is happening !

Density was the issue here, I needed to have this in the AndroidManifest.xml

<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"
/>

The most important is the android:anyDensity =" true ".

Don't forget to add the following in the AndroidManifest.xml for every activity (for Android 4.1 and below):

android:configChanges="locale"

This version is needed when you build for Android 4.2 (API level 17) explanation here:

android:configChanges="locale|layoutDirection"

Changing Locale within the app automatically to english

I created new BaseActivity.class and extended that class to Home Screen,

public abstract class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
}

protected abstract int getLayoutResourceId();


@Override
protected void attachBaseContext(Context newBase) {
String language = AppSettings.getInstance(newBase).getLanguage();
super.attachBaseContext(LocaleHelper.wrap(newBase, language));
}


private static class LocaleHelper extends ContextWrapper {

public LocaleHelper(Context base) {
super(base);
}

public static ContextWrapper wrap(Context context, String language) {
if (TextUtils.isEmpty(language.trim())) {
return new LocaleHelper(context);
}
Configuration config = context.getResources().getConfiguration();
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
//noinspection deprecation
config.locale = locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLayoutDirection(locale);
context = context.createConfigurationContext(config);
} else {
//noinspection deprecation
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
return new LocaleHelper(context);
}

} // LocaleHelper

}

Here is the code modification on HomeActivity,

public class HomeActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_home);
ButterKnife.bind(this);
}

@Override
protected int getLayoutResourceId() {
return R.layout.activity_home;
}


}

Android - Changing Locale language within the app itself

use this

 Locale.setDefault("Your Locale");

Changing locale within the app effectively

Seems a bit too early to post an answer to my question, but adding Locale.setDefault(newLocale) to the code above solves all the three problems mentioned.

Change app language programmatically in Android

It's possible. You can set the locale. However, I would not recommend that. We've tried it at early stages, it's basically fighting the system.

We have the same requirement for changing the language but decided to settle to the fact that UI should be same as phone UI. It was working via setting locale but was too buggy. And you have to set it every time you enter activity (each activity) from my experience. here is a code if you still need this (again, I don't recommend that)

Resources res = context.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.setLocale(new Locale(language_code.toLowerCase())); // API 17+ only.
// Use conf.locale = new Locale(...) if targeting lower versions
res.updateConfiguration(conf, dm);

If you have language specific content - you can change that base on the setting.


update on 26th of march 2020

    public static void setLocale(Activity activity, String languageCode) {
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Resources resources = activity.getResources();
Configuration config = resources.getConfiguration();
config.setLocale(locale);
resources.updateConfiguration(config, resources.getDisplayMetrics());
}
  • NOTES: Language code cannot got '-' & must be 2 small case letter only


Related Topics



Leave a reply



Submit