Last active
November 24, 2021 12:14
-
-
Save Longwater1234/d1f913008a3a0a70a4e112d501b3c8b6 to your computer and use it in GitHub Desktop.
Android Language Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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