-1

i'm using below code to change my app locale. this works fine in android O and later but not working in android N and older versions. where is problem?

public class CustomContextWrapper extends ContextWrapper {

    private CustomContextWrapper(Context base) {
        super(base);
    }

    public static CustomContextWrapper wrap(Context context, String lang) {

        Locale locale;

        if (lang.length() > 2) {
            String[] langWithRegion = lang.split("-");
            locale = new Locale(langWithRegion[0], langWithRegion[1]);
        } else
            locale = new Locale(lang);

        Locale.setDefault(locale);

        Configuration configuration = context.getResources().getConfiguration();
        configuration.setLocale(locale);
        configuration.setLayoutDirection(locale);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);
        }

        return new CustomContextWrapper(context.createConfigurationContext(configuration));
    }
}

my Application class

@Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(CustomContextWrapper.wrap(base,"fa"));
    }

and my BaseActivity

@Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(CustomContextWrapper.wrap(base,"fa"));
    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ali Zarei
  • 3,523
  • 5
  • 33
  • 44

1 Answers1

-1

OK , after many hours search and test solutions in stackoverflow finally i found this thread. it seems there is a problem (or maybe a behavior change) in appcompat 1.1.0. by downgrading appcompat to v1.0.2 and material to v1.0.0 problem fixed.

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
Ali Zarei
  • 3,523
  • 5
  • 33
  • 44