Skip to content

Commit

Permalink
#21: After SettingsActivity#changeLocale the locale of all open paren…
Browse files Browse the repository at this point in the history
…t Activities are also updated
  • Loading branch information
k3b committed Jan 8, 2016
1 parent c72e67e commit ee8b9a4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.io.File;

import de.k3b.android.widget.AboutDialogPreference;
import de.k3b.android.widget.LocalizedActivity;
import uk.co.senab.photoview.HugeImageLoader;
import uk.co.senab.photoview.PhotoViewAttacher;
Expand All @@ -55,7 +56,7 @@ protected void onCreate(final Bundle savedInstanceState) {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setLanguage((String) newValue);
SettingsActivity.super.recreate();
LocalizedActivity.recreate(SettingsActivity.this);
return true; // change is allowed
}
});
Expand Down Expand Up @@ -188,6 +189,9 @@ public static void show(Activity parent) {
private void updateSummary() {
final String languageKey = prefsInstance.getString(Global.PREF_KEY_USER_LOCALE, "");
setLanguage(languageKey);
AboutDialogPreference about =
(AboutDialogPreference) findPreference("about");
about.setTitle(AboutDialogPreference.getAboutTitle(this));
}

private void setLanguage(String languageKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public AboutDialogPreference(Context context, AttributeSet attrs) {
}

@NonNull
private static String getAboutTitle(Context context) {
public static String getAboutTitle(Context context) {
return context.getString(R.string.about_summary,context.getString(R.string.version_postfix));
}

Expand Down
49 changes: 41 additions & 8 deletions app/src/main/java/de/k3b/android/widget/LocalizedActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,60 @@
* Created by k3b on 07.01.2016.
*/
public class LocalizedActivity extends Activity {
/** if myLocale != Locale.Default : activity must be recreated in on resume */
private Locale myLocale = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
fixLocale(this);
super.onCreate(savedInstanceState);
}

@Override
protected void onResume() {
super.onResume();

// Locale has changed by other Activity ?
if ((myLocale != null) && (myLocale.getLanguage() != Locale.getDefault().getLanguage())) {
myLocale = null;
recreate();
}
}

/**
* Set Activity-s locale to SharedPreferences-setting.
* Must be called before
* @param context
*/
public static void fixLocale(Context context)
{
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
String language = prefs.getString(Global.PREF_KEY_USER_LOCALE, "");
Locale locale = Global.systemLocale;
Locale locale = Global.systemLocale; // in case that setting=="use android-locale"
if ((language != null) && (!language.isEmpty())) {
locale = new Locale(language);
locale = new Locale(language); // overwrite "use android-locale"
}

if (locale != null) {
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
Resources resources = context.getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
// recreate();

if (context instanceof LocalizedActivity) {
((LocalizedActivity) context).myLocale = locale;
}
}
}

public static void recreate(Activity context) {
while (context != null) {
context.recreate();
context = context.getParent();
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
Resources resources = context.getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
// recreate();

}
}

0 comments on commit ee8b9a4

Please sign in to comment.