Skip to main content

Change language programatically in Android



Is it possible to change the language of an app programmatically while still using Android resources?





If not, is it possible to request a resource in an specific language?





I would like to let the user change the language of the app from the app.


Comments

  1. It's possible. You can set the locale. However, i would not recommend that. We've tried it at early stages, it's basically fighting the system. We have same requirement for changing language, but decided to settle to the fact that UI should be same as phone UI. It was working via setting locale but was too buggy. And you have to set it every time you enter activity ( each activity ) from my experience. here is a code if you still need this ( again, i don't recommend that )

    Resources res = context.getResources();
    // Change locale settings in the app.
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(language_code.toLowerCase());
    res.updateConfiguration(conf, dm);


    If you have language specific content - you can change that base on the setting.

    ReplyDelete
  2. If u write

    android:configChanges="locale"

    in every activity than no need to set it every time you enter activity

    ReplyDelete
  3. I was looking for a way to change the system language programmatically.
    While I fully understand that a normal application should never do that and instead either:


    the user should be pointed(through an intent) to the system settings to change it manually
    the application should handle its localization on its own just like described in the answer of Alex


    there was a need to really change the language of the system programmtically.

    This is undocumented API and thus should not be used for market/end-user applications!

    Anyway heres the solution i found:

    Locale locale = new Locale(targetLocaleAsString);

    Class amnClass = Class.forName("android.app.ActivityManagerNative");
    Object amn = null;
    Configuration config = null;

    // amn = ActivityManagerNative.getDefault();
    Method methodGetDefault = amnClass.getMethod("getDefault");
    methodGetDefault.setAccessible(true);
    amn = methodGetDefault.invoke(amnClass);

    // config = amn.getConfiguration();
    Method methodGetConfiguration = amnClass.getMethod("getConfiguration");
    methodGetConfiguration.setAccessible(true);
    config = (Configuration) methodGetConfiguration.invoke(amn);

    // config.userSetLocale = true;
    Class configClass = config.getClass();
    Field f = configClass.getField("userSetLocale");
    f.setBoolean(config, true);

    // set the locale to the new value
    config.locale = locale;

    // amn.updateConfiguration(config);
    Method methodUpdateConfiguration = amnClass.getMethod("updateConfiguration", Configuration.class);
    methodUpdateConfiguration.setAccessible(true);
    methodUpdateConfiguration.invoke(amn, config);

    ReplyDelete
  4. It's really work... fa=Presian... en=English...
    enter your language code in languageToLoad :

    import android.app.Activity;
    import android.content.res.Configuration;
    import android.os.Bundle;

    public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String languageToLoad = "fa"; // your language
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
    getBaseContext().getResources().getDisplayMetrics());
    this.setContentView(R.layout.main);
    }
    }

    ReplyDelete
  5. Alex Volovoy answer only works for me if it's in onCreate method of the activity.

    The answer that works in all the methods is in another thread

    Change language programatically in Android

    Here is the adaptation of the code



    Resources standardResources = getBaseContext().getResources();

    AssetManager assets = standardResources.getAssets();

    DisplayMetrics metrics = standardResources.getDisplayMetrics();

    Configuration config = new Configuration(standardResources.getConfiguration());

    config.locale = new Locale(languageToLoad);

    Resources defaultResources = new Resources(assets, metrics, config);



    Hope that it helps.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?