setLocale for change language has no effect in Android

Tags: , , , ,



If Generated Signed Bundle – Release, i failed to change the language of an app programmatically on some Android devices..
but if Build Apk(s), it’s work on all Android devices..


This is a list of devices that have been tested :

✓ Samsung J7 prime

✓ Samsung A50

✓ Vivo y81

x Xiaomi Redmi Note 8


This is a resource that i use :

  • values (for default)
  • values-en
  • values-en-rUS
  • values-es-rES
  • values-in
  • values-in-rID

And this is the code that I use :

(Application – App)

public class App extends Application {    

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        new cgt_Languages(getApplicationContext());
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        new cgt_Languages(base);
    }
}

(Class – cgt_Languages)

public cgt_Languages(Context context) {
    try {
        //Check exists database
        cgt_pm_Languages pmLanguages = new cgt_pm_Languages(context);

        String idioma;
        String baseLocale;
        if ("in".equals(pmLanguages.getLanguage())) {
            idioma = "in";
            baseLocale = "ID";
        } else if ("es".equals(pmLanguages.getLanguage())) {
            idioma = "es";
            baseLocale = "ES";
        } else {
            idioma = "en";
            baseLocale = "US";
        }

        Locale locale = new Locale(idioma, baseLocale);
        Locale.setDefault(locale);
        Configuration configuration = new Configuration();
        configuration.setLocale(locale);

        // create configuration
        context.createConfigurationContext(configuration);

        // update configuration
        context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
    } catch (Exception e) {
        new cgt_ErrorMessage(context, e.toString(), "cgt_Languages - cgt_Languages", true, false).startErrorMessage("");
    }
}

(Fragment – fgtSetting)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    new cgt_Languages(getContext());

    in.setOnClickListener(view1 -> {
        String idioma = "in";
        String baseLocale = "ID";

        Locale locale = new Locale(idioma, baseLocale);
        Locale.setDefault(locale);
        Configuration configuration = new Configuration(context.getResources().getConfiguration());

        if (Build.VERSION.SDK_INT >= 17) {
            configuration.setLocale(locale);
            context = context.createConfigurationContext(configuration);
        } else {
            configuration.locale = locale;
            context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
        }


        Intent intent = activity.getIntent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
        activity.finish();
        activity.overridePendingTransition(0, 0);
    });
}

I need your help..

Answer

This answer worked for me :

bundle  {
    language {
    // Specifies that the app bundle should not support
    // configuration APKs for language resources. These
    // resources are instead packaged with each base and
    // dynamic feature APK.
    enableSplit = false
    }
}

I have got the answer from this



Source: stackoverflow