Skip to content Skip to sidebar Skip to footer

Android Localization Problem: Not All Items In The Layout Update Properly When Switching Locales

Here's the problem: When I have an activity running in the background, and I switch locales, and I switch back to the application, everything updates... EXCEPT checkboxes and radio

Solution 1:

The cause of the problem is that CompoundButton.onSaveInstanceState() calls setFreezesText(true) and thus saves&restores the text.

A simple solution is using a subclass like this:

publicclassCheckBoxNoPersistentTextextendsCheckBox {

    publicCheckBoxNoPersistentText(final Context context) {
        super(context);
    }

    publicCheckBoxNoPersistentText(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    publicCheckBoxNoPersistentText(final Context context, final AttributeSet attrs, finalint defStyle) {
        super(context, attrs, defStyle);
    }

    @OverridepublicvoidonRestoreInstanceState(final Parcelable state) {

        finalCharSequencetext= getText(); // the text has been resolved anewsuper.onRestoreInstanceState(state); // this restores the old text

        setText(text); // this overwrites the restored text with the newly resolved text

    }
}

Solution 2:

That is a fascinating bug. I can reproduce it on my Nexus One.

It seems to be in the default implementation of onSaveInstanceState(). If you override that to be a no-op (do not chain to the superclass), the problem goes away.

The default onSaveInstanceState() is supposed to handle stuff like the checkbox state, but they must have botched that and are saving the text, too.

So, you have a couple of workarounds:

  1. Override onSaveInstanceState() and do not chain to the superclass. This, however, eliminates any automatic state-saving you would ordinarily get.
  2. In onRestoreInstanceState() (...I think...), after chaining to the superclass, call setText() on your affected widgets with the proper string resource, to reset it back to the right value.

I will try to follow up on this more tomorrow when I get a chance. I want to check the source code and probably file this as an issue.

Solution 3:

If some of these are in memory they will not change. If you reboot the phone, reinstall the application or at least completely kill the app it will work fine.

Solution 4:

This 2 years old ticket proposes a workaround of not using the android:id so I fixed this issue by using a similar layout:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"><!-- KEEP THIS ALWAYS THE FIRST because it dosen't have
        an android:id as a workaround of this bug
        https://code.google.com/p/android/issues/detail?id=13252
        --><RadioButtonxmlns:android="http://schemas.android.com/apk/res/android" /><!-- other elements --></RelativeLayout>

So now to get the RadioButton I use something like this:

private RadioButton getButton(RelativeLayout layout) {
    RadioButtonbutton=null;
    if (layout.getChildCount() != 0) {
        button = (RadioButton) layout.getChildAt(0);
    }
    return button;
}

So I can set the properties programmatically.

Post a Comment for "Android Localization Problem: Not All Items In The Layout Update Properly When Switching Locales"