2

I have 3 buttons to change language. So I need to make method, where I could set listeners to buttons which could change language everytime they are pressed. I tried this https://stackoverflow.com/a/2900144/1088229 but this way it changes locale only once and seems that this doesn't work any more (if i click again, it doesn't even respond). So I added changeLanguageListener() in end of listener, so listener is refreshed.

So is it ok, how i solved it or there is another way?

private void changeLanguageListener() {
    final Button butEn = (Button) findViewById(R.id.button_language_en);
    final Button butLt = (Button) findViewById(R.id.button_language_lt);
    final Button butRu = (Button) findViewById(R.id.button_language_ru);
    OnClickListener listener = new OnClickListener() {

        public void onClick(View v) {
            Button but = (Button) v;
            Resources res = getResources();
            String current = res.getConfiguration().locale.getCountry();
            Log.i("Current", current);
            String localeString = new String(current);
            if (but.equals(butEn)) {
                localeString = "en";
            } else if (but.equals(butLt)) {
                localeString = "lt";
            } else if (but.equals(butRu)) {
                localeString = "ru";
            }
            Log.i("Clicked", localeString);

            if (!current.equalsIgnoreCase(localeString) && localeString.length() > 0) {
                // Change locale settings in the app.
                DisplayMetrics dm = res.getDisplayMetrics();
                android.content.res.Configuration conf = res.getConfiguration();
                conf.locale = new Locale(localeString.toLowerCase());
                res.updateConfiguration(conf, dm);  
                //refresh menu
                setGridView();
                //added this line to get refreshed listener
                changeLanguageListener();
            }               
        }
    };
    butEn.setOnClickListener(listener);
    butLt.setOnClickListener(listener);
    butRu.setOnClickListener(listener);
}
Community
  • 1
  • 1
Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41

3 Answers3

2

So, because of only two activities (two menu modes) where i let to change locale, I just restart Activity right after locale change, so everything is refreshed. Didn't find any other better solution

Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41
2
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.## Heading ##

aizaz
  • 3,056
  • 9
  • 25
  • 57
Ramkailash
  • 252
  • 3
  • 5
1

I would not recommend to change the language within your application. A better solution would be a button that opens the systems locale settings. You can use the following intent for this.

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCALE, 0);

For more info about the locale settings: http://developer.android.com/reference/android/provider/Settings.html#ACTION_LOCALE_SETTINGS

Moss
  • 6,002
  • 1
  • 35
  • 40
  • The main reason for changing location programmatically is that in language packs there no language I need. For example, my phone has baltic language pack, so it has lots of languages, including lithuanian (mine), english, but there is no russian (which we need for application too). – Paulius Vindzigelskis Jun 20 '12 at 08:01
  • 2
    @Moss: _"I would not recommend to change the language within your application"_ Why not? There are many instances where an application can change a language to help an end-user. For example, a first-aid app showing the language a tourist speaks to allow them to understand paramedic's instructions while they are being treated for injuries in a foreign land. – ChuongPham Jul 29 '14 at 11:28
  • 1
    @ChuongPham: your case is indeed a very good example on where a in-app changes is really useful and required. But in the general case I prefer using the system to handle in which language the app should be. – Moss Jul 29 '14 at 11:34