Skip to content

Instantly share code, notes, and snippets.

@Longwater1234
Last active November 24, 2021 12:14
Show Gist options
  • Save Longwater1234/d1f913008a3a0a70a4e112d501b3c8b6 to your computer and use it in GitHub Desktop.
Save Longwater1234/d1f913008a3a0a70a4e112d501b3c8b6 to your computer and use it in GitHub Desktop.
Android Language Manager
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import java.util.Locale;
/**
* Language Manager.
* Changing the global display Language of your App
*/
public class LanguageManager {
public static void setAppLocale(String language, Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Resources resources = activity.getResources();
Configuration configuration = resources.getConfiguration();
configuration.setLocale(new Locale(language));
activity.getApplicationContext().createConfigurationContext(configuration);
} else {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = activity.getResources().getConfiguration();
config.setLayoutDirection(locale);
config.setLocale(locale);
activity.getResources().updateConfiguration(config, activity.getResources().getDisplayMetrics());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment