2

I wrote a little test application for learning how to work with multiple languages and I used the emulator to test it. I am using NetBeans with the Android SDK. The multi-language aspect of the app is working fine. Because of the slowness of using the emulator, I tried to run my app on my device (HTC Inspire 4G). I've done before with other applications and had no problem. However, today, with this simple app, when I switch back and forth between the first and second screens, the buttons and textview on page 2 keep shrinking. This does not happen in the emulator; the widgets stay the same size. I'm not doing anything layout-wise that I haven't done in the past so I have no clue as to why the buttons are going whacko. Relevant code is posted below. I hope another pair of eyes might find what I'm missing.

Thanks times a million, Bill

UPDATE: I noticed if I rotate my phone, from portrait to landscape (or vice versa) after the buttons have shrunk, they revert to normal default size. (Though the language resets to the default, English.)

MainActivity.java:

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button;

button = (Button) findViewById(R.id.use_english_button);
button.setOnClickListener(new GoToPage2OnClickListener("en"));

button = (Button) findViewById(R.id.use_french_button);
button.setOnClickListener(new GoToPage2OnClickListener("fr"));

button = (Button) findViewById(R.id.use_spanish_button);
button.setOnClickListener(new GoToPage2OnClickListener("es"));

button = (Button) findViewById(R.id.use_italian_button);
button.setOnClickListener(new GoToPage2OnClickListener("it"));
}

private class GoToPage2OnClickListener implements View.OnClickListener {
private String language = "";

public GoToPage2OnClickListener(String language) {
    this.language = language;
}

public void onClick(View v) {

    // This comes from
    // http://stackoverflow.com/questions/12230553/android-how-to-change-the-application-language-at-runtime

    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
        getBaseContext().getResources().getDisplayMetrics());

    SharedPreferences languagepref =
        getSharedPreferences("language", MODE_PRIVATE);
    SharedPreferences.Editor editor = languagepref.edit();
    editor.putString("languageToLoad", language);
    editor.commit();

    Intent intent = new Intent(MainActivity.this,
        Page2Activity.class);
    startActivity(intent);
}
}

Page2Activity.java:

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

setContentView(R.layout.page_2);

SharedPreferences language =
    getSharedPreferences("language", MODE_PRIVATE);

Map<String,String> prefMap = (Map<String,String>) language.getAll();
String str = prefMap.get("languageToLoad");

Locale locale = new Locale(str);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;

DisplayMetrics dm = getBaseContext().getResources().getDisplayMetrics();

getBaseContext().getResources().updateConfiguration(config,
    getBaseContext().getResources().getDisplayMetrics());

Button button;

button = (Button) findViewById(R.id.page_2_back_button);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        finish();
    }
});

page_2.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/output_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="@string/welcome_b"
/>
<TableLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
        android:id="@+id/page_2_back_button"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/page_2_back_button"
        android:layout_margin="5sp"
        android:layout_weight="1"
        />
    <Button
        android:id="@+id/page_2_next_button"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/page_2_next_button"
        android:layout_margin="5sp"
        android:layout_weight="1"
        />
</TableRow>
</TableLayout>
Bill Doss
  • 601
  • 1
  • 6
  • 13

1 Answers1

1

I figured it out!! After each:

Configuration config = new Configuration();

you have to call:

config.setToDefaults();

From http://developer.android.com/reference/android/content/res/Configuration.html

Public Constructors Configuration() Construct an invalid Configuration. You must call setToDefaults() for this object to be valid.

I will have to post this to the question/answer where I learned how to set the locale.

Bill Doss
  • 601
  • 1
  • 6
  • 13