0

I'm familiar with Java, but just learning Android Programming. I liked the color bars in their Hello, LinearLayout example quite a bit, so I've been trying to expand that into a little program I can use with my 13 month old. I have it displaying seven colors with labels and four rows. One thing I'd like to do (since I'm bilingual), is to be able to change the Locale display language on the fly, without quitting the app, loading a new locale, and starting the app again (which works, but it's tedious). So the seven color bars are labelled appropriate to the selected language, but I would like those to be able to change with a click within the program, instead of exiting the program. In other words, I really only want to change Locale within the scope of the app, NOT the entire phone.

I've found a couple of tips on Locale changes here, but nothing that quite 100% works in this instance. What I've done is to combine Spinner code with the layout. I'm trying to keep everything in one file (for now) just so that I know the variables and scopes are all working (I had tried passing things back and forth just as in the "official" Android HelloSpinner code, and that made nothing but a mess). Here's what I have coded so far for my custom version of HelloLinearLayout:

Any and all advice appreciated! Oh and I'm building and testing against Gingerbread, since that's what my HTC phone runs.

package net.buellnet.hellolinearlayout;

import java.util.Locale;

import android.app.Activity;<br/>
import android.content.res.Configuration;<br/>
import android.os.Bundle;<br/>
import android.view.View;
import android.widget.AdapterView;<br/>
import android.widget.ArrayAdapter;<br/>
import android.widget.Spinner;<br/>
import android.widget.Toast;<br/>

public class HelloLinearLayoutActivity extends Activity {<br/>
    /** Called when the activity is first created. */<br/>
    @Override<br/>
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.lang_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);
        spinner.setHapticFeedbackEnabled(true);
        spinner.setOnItemSelectedListener(null);
    }

           public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

                String language = parent.getItemAtPosition(pos).toString();
                Configuration newConfig = new Configuration();

                Toast.makeText(parent.getContext(), "The language is " +
                          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();<br/>
/* I threw the above line in just to see if the control was working. I can change the "language" but the Toast line never pops up, nor does the locale ever change. */

                if (language.equals("English")) {
                    newConfig.locale = Locale.ENGLISH;
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
                else if (language.equals("French")) {
                    newConfig.locale = Locale.FRENCH;
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
                else if (language.equals("German")) {
                    newConfig.locale = Locale.GERMAN;
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
                else if (language.equals("Italian")){
                    newConfig.locale = Locale.ITALIAN;
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
                else if (language.equals("Portuguese")) {
                    newConfig.locale = new Locale("pt");
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
                else if (language.equals("Spanish")) {
                    newConfig.locale = new Locale("es");
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
            }

            public void onNothingSelected(AdapterView parent) {
                  // Do nothing.
            }
}
</code>
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501

2 Answers2

0
 try updateConfiguration function to set new Locale........


  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);

in place of spinner.setOnItemSelectedListener(null);

1 - public class HelloLinearLayoutActivity extends Activit implements Spinner.OnItemSelectedListener
2- spinner.setOnItemSelectedListener(this);
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • My problem seems to be that the spinner listener (if I can call it that) isn't actually responding to any changes when I trigger it. I can press the dropdown, it starts with a blank, select 'German' and it will say German at the bottom of my screen (this is all in my layout/main.xml) but it won't actually change anything to BE in German. – John Buell Jun 15 '12 at 22:00
  • 1
    Ah, that's getting closer - the Toast popup code works now. Thanks! – John Buell Jun 15 '12 at 22:08
0

Try it this way! See below code (notice that setContentView is not inside the onCreate method anymore):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    doUIStuff();
    //otherThanUILogicGoesHere
}

@Override
public void onResume() {
    super.onResume();
    if (JustModifiedLocale){
        doUIStuff();
    }
}

void doUIStuff(){
    setContentView(R.layout.my_layout);
    //other UI_related necessary initializations go here
}