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
How to Open a Url in Android'S Web Browser from My Application
How to Start My App When the Phone Starts on Android
Re-Launch of Activity on Home Button, But...Only the First Time
How to Have Android Service Communicate With Activity
How to Use Inputfilter to Limit Characters in an Edittext in Android
Drawing a Line/Path on Google Maps
Run/Install/Debug Android Applications Over Wi-Fi
Volley Jsonobjectrequest Post Request Not Working
"Android.View.Windowmanager$Badtokenexception: Unable to Add Window" on Buider.Show()
Using the Android Application Class to Persist Data
How to Do Something Like a Flowlayout in Android
What Does @Hide Mean in the Android Source Code
Passing Android Bitmap Data Within Activity Using Intent in Android
How to Start a Service When .Apk Is Installed For the First Time
Android 8.0: Java.Lang.Illegalstateexception: Not Allowed to Start Service Intent
Android Basics: Running Code in the Ui Thread
Can't Find Theme.Appcompat.Light For New Android Actionbar Support