1

I can't seem to make the Autofill to show for an application that handles its own localization.

For some reason, if attachBaseContext is modified to configure localized resources, the Autofill simply won't work:

if attachBaseContext is modified

However, if the activity just has setContentView(R.layout.activity_main) and nothing else, the Autofill works fine:

without attachBaseContext

Does anyone know why this happens? And how do I localize my app while allowing Autofill to run normally?


Below are some of the code for this test app.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        if (newBase != null) {
            // Switch the locale between "en" or "in"
            final Context newContext = wrapLocale(newBase, new Locale("en"));
            super.attachBaseContext(newContext);
        } else {
            super.attachBaseContext(newBase);
        }
    }

    private Context wrapLocale(Context context, Locale locale) {
        final Resources res = context.getResources();
        final Configuration config = res.getConfiguration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            final LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            config.setLocales(localeList);
            config.setLocale(locale);
            return context.createConfigurationContext(config);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(locale);
            return context.createConfigurationContext(config);
        } else {
            config.locale = locale;
            res.updateConfiguration(config, null);
            return context;
        }
    }
}

res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/sample_string" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:autofillHints="name"
        android:hint="@string/sample_hint" />

</LinearLayout>

res/values/strings.xml

<resources>
    <string name="app_name">AutofillAndLocale</string>
    <string name="sample_string">English</string>
    <string name="sample_hint">This is in English</string>
</resources>

res/values-in/strings.xml

<resources>
    <string name="app_name">AutofillAndLocale</string>
    <string name="sample_string">Bahasa Indonesia</string>
    <string name="sample_hint">Pakai Bahasa Indo</string>
</resources>
4

0 回答 0