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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.