Change App Language in devices with API 22 String don't change

67 Views Asked by At

I want to change String when change app locale

there is Profile Fragment

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.changeLanguageButton:
                if (PrefManager.getLocale(getActivity()).equals("en")) {
                    setLocale("ar");
                } else {
                    setLocale("en");
                }
                break;
        }
    }

    public void setLocale(String lang ) {
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        config.setLayoutDirection(locale);
        getActivity().getBaseContext().getResources().updateConfiguration(config, getActivity().getApplication().getResources().getDisplayMetrics());
        PrefManager.saveLocale(getActivity(), lang);

        Intent intent = new Intent(getActivity(), SplashActivity.class);
        startActivity(intent);

    }

There is Splash Activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (PrefManager.getLocale(this) == null){
            setLocale("en");
        }else if (PrefManager.getLocale(this).equals("en")){
            setLocale("en");
        }else {
            setLocale("ar");
        }

        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
                Splash.this.startActivity(mainIntent);
                Splash.this.finish();
            }
        }, 3000);

    }

    public void setLocale(String lang) {
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        config.setLayoutDirection(locale);
        this.getBaseContext().getResources().updateConfiguration(config, this.getApplication().getResources().getDisplayMetrics());
        PrefManager.saveLocale(this, lang);
    }

The Issue is This code Work in device with API 23 and translate all strings but not work in device with API 22 it just change layout direction only

My minSDKVersion 19 & targetSDKVersion 29

1

There are 1 best solutions below

0
On BEST ANSWER

I found the answer if anyone interested

I changed the Arabic String file name from "string (ar-rEG) " to "string (ar)"

and it works fine