Google Map Caching Issue - android

I am working with google map in my activity.
When i change language for example Chinese in my app settings
the langauge of map did not change till i reopen my app.
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.activity_maps);
i searched a lot about this issue i found that it is google map caching issue but did not found any solution.

I can think about 2 options to solve this:
Calling setContentView in the onConfigurationChanged method
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (language_changed){//pseudo_code
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.activity_maps);
//now you need to do again all the findViewById call
}
}
Restart the activity in the onConfigurationChanged method
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (language_changed){//pseudo_code
startActivity(CurrentActivity.this, CurrentActivity.class);
finish();
}
}
I've not tested this, but maybe it's a starting point to try to solve your problem.
Don't forget to add android:configChanges="locale" to your activity tag in the AndroidManifest.xml to let the system know that you want to handle Locale changes by yourself. Read more here if needed

You can use BroadcastReceiver with LOCALE_CHANGED Intent to detect setting change, and then update your view (Map view or total layout).
https://developer.android.com/reference/android/content/Intent.html#ACTION_LOCALE_CHANGED
public class LocaleReceiver extends BroadcastReceiver {
public LocaleReceiver() {}
#Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())){
// Call a method to trigger view refreshing here
}
}
}

Related

How to Refresh App with onConfigChanges without loosing instance

protected void onResume() {
super.onResume();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String lang = settings.getString("lang_list", "");
// Log.d("Lang",lang);
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
recreate();
Locale locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
if (AppController.getInstance().isPreferenceChanged()) {
setupViewPager(viewPager);
adapter.notifyDataSetChanged();
}
}
I want to change the app language without recreate the app. I mean I want to do it when user select any language from the setting then it Should change the Setting Activity language without using recreate();, because when I use recreate(); it blinks the App once.
So I am not using recreate();. Instead I wrote below code in Setting Activity in AndroidManifest.xml
<activity
android:name=".activity.SettingsActivity"
android:label="#string/title_activity_settings"
android:configChanges="orientation|keyboardHidden|screenSize|locale|layoutDirection"/>
As in below screenshot you can see that I have selected "Hindi" for the language but it is not updating the Activity to Hindi. I mean "Select Country", "Select Your Language" and "Select Categories" should be display in Hindi instead of English. I have wrote String in both languages.
Can Anybody know How to change it when Change the language ?
OR
Why onConfigChanges is not working for locale as it is working for Orientation.
Thank you !
In your MainActivity call sharedPrefrences on onStart callback insted of calling in onCreate
Example:-
protected void onStart() {
SharedPreferences sharedPreferences = this.getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE);
String pine = sharedPreferences.getString("language", DEFAULT);
String languageToLoad = pine;
Locale locale = new Locale(languageToLoad);//Set Selected Locale
Locale.setDefault(locale);//set new locale as default
Configuration config = new Configuration();//get Configuration
config.locale = locale;//set config locale as selected locale
this.getResources().updateConfiguration(config, this.getResources().getDisplayMetrics());
invalidateOptionsMenu(); //This changes language in OptionsMenu too
setTitle(R.string.app_name); //calling app name according to language selected by user
super.onStart();
}
You can use a singleton class that will handle the language change without restarting the activity,
public class MyApplication extends Application
{
private Locale locale = null;
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (locale != null)
{
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
#Override
public void onCreate()
{
super.onCreate();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String lang = settings.getString(getString(R.string.pref_locale), "");
if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
{
locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
}
}

onConfigurationChanged is not being called on language change

Although I added to the manifest file, for each activity,
android:configChanges="locale|layoutDirection"
the method is not being called, when I change the language inside the application.
Code to change the langauge
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
// Configuration: a class describing all device configration information that can impact resources the apllication retrieves.
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
I know it is not called because I am printing to logcat and displaying a message and none is being executed
#Override
public void onConfigurationChanged (Configuration newConfig){
printMyLogCat("in Onconfigarion changed");
doMyToast(this, "in Onconfigarion changed");
super.onConfigurationChanged(newConfig);
}
public void printMyLogCat(String string) {
Log.i(getClass().getName(), string);
}
public void doMyToast(Context context, String string){
Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
}

Use custom locale for plurals

I am trying to override the locale to use Czech locale no matter what is set in the phone. However, although when I try to get the current locale, it returns czech, but for plurals, it acts like it still takes the real phone locale.
Here is my code:
<application
android:name=".xxx"
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:theme="#style/Theme.mystyle"
android:configChanges="locale"
>
In application:
String lang = settings.getString("cs", "");
locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
I posted on GitHub simple LocaleAssistant class I just tested with plurals for Slovak and also Czech language, and it worked on the device with default locale set to EN. Just call those two methods from within your custom Application on the member instance of LocaleAssistant.
In the end I found the correct solution. Gray Wolf's solution did not work for me for some reason. Maybe it is all about the API level you develop your app for.
This is the working code. Besides that, it is also necessary to add configChanges:locale to AndroidManifest. This should work on Android 4.0.1 and above.
public class MyApplication extends Application
{
private Locale locale = null;
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (locale != null)
{
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
#Override
public void onCreate()
{
super.onCreate();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String lang = "cs";
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang))
{
locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
}
}

How to reload the current view when I change the locale in app in android

I am currently work with changing language in the app. My app structure is tab host + fragment I have successfully change the locale but it is quite strange.
That means after I run the change locale code , it does not change the view immediately but only when I go to another tab. I think this is due to I need to reload the view, but are there any way to implment this without kill and restart the activity?
Because there is some goolge analytic code, the entry number will be increase if the user start activity again? Are there standard way to reload view? thanks
The change locale function is in one of the tabhost fragment, I have to refresh the view in tabhost (main activity), and the current fragment .
public OnClickListener setChangeLangListener(final String lang) {
OnClickListener changeLangListener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Configuration config = new Configuration(getResources()
.getConfiguration());
if (lang.equals("en")) {
config.locale = Locale.ENGLISH;
chi.setTextColor(oldColor);
eng.setTextColor(getActivity().getResources().getColor(android.R.color.white));
} else {
config.locale = Locale.TRADITIONAL_CHINESE;
eng.setTextColor(oldColor);
chi.setTextColor(getActivity().getResources().getColor(android.R.color.white));
}
getResources().updateConfiguration(config,
getResources().getDisplayMetrics());
}
};
return changeLangListener;
}
eng.setOnClickListener(setChangeLangListener("en"));
chi.setOnClickListener(setChangeLangListener("zh"));
Allright add this to your manifest
android:configChanges="locale"
and override onConfigurationChanged() in your activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
// refresh your views here
super.onConfigurationChanged(newConfig);
}
go here for more info.
Hope it helps. :)
Have you tried calling setContentView. Eg.:
String languageToLoad = "fr"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
activity.setContentView(R.layout.your_layout);

Activity is blinking after locale change in Android 4.1+

I have implemented custom locale selection about a year ago but after 4.1 release users start to complain on constant activity blinking. Here is code I'm using (compiled from different SO answers):
public final class TestApplication extends Application
{
private Locale desiredLocale = new Locale("ru-RU");
#Override
public void onCreate() {
super.onCreate();
updateLocale(new Configuration());
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
updateLocale(newConfig);
}
private void updateLocale(Configuration newConfig) {
newConfig.locale = desiredLocale;
Locale.setDefault(desiredLocale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
Application contains only one empty activity, which is recreated by Android every second after I change device orientation. Here is the source of sample.
It looks like all applications which use this technique became invalid. What is the correct approach?
This line caused solution to fail:
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
Correct (at least it works) implementation is defined here
https://stackoverflow.com/a/14010044/554336 :
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
So new configuration instance should be created every time.

Categories

Resources