Alister Geek
Alister Geek

Reputation: 53

How to Change the default Language in an App - Android Studio

I have been working with an App. All looked good until I install it. Its default language is set to Arabic. I can although change the language from the drop-down but it's not cool for the people who will work on it. Please help me set the default language to English programmatically.

Thanks! :)

Upvotes: 0

Views: 672

Answers (1)

Jacks
Jacks

Reputation: 828

You can set the locale of the app, to a desired one - But you will have to fetch it from some source if you want to set it automatically(for this example from device locale you can get it)

This is how you can get the current Locale

Locale getCurrentLocale(Context context){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            return context.getResources().getConfiguration().getLocales().get(0);
        } else{
            //noinspection deprecation
            return context.getResources().getConfiguration().locale;
        }
    }

Here is how you can set it default.

private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        if (Build.VERSION.SDK_INT >= 17) {
            config.setLocale(locale);
            context = context.createConfigurationContext(config);
        } else {
            config.locale = locale;
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return context;
    }

Upvotes: 1

Related Questions