0

I'm currently developing an application which supports 3 languages base on Yaroslav Berezanskyi codes. It's great and I can change application language at runtime but there is a problem which has confused me. Unfortunately when I want to go to ActivityPreference, the language and resources still remain in English and it doesn't use Right-to-Left resources such as layouts and strings when I change the app language to Farsi. What is the problem with Preference v7? I have attached some images from app when the language is English and Farsi. Thank in advance.

Change language method

private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        if (Build.VERSION.SDK_INT >= 17) {
            config.setLocale(locale);
            context = context.createConfigurationContext(config);
        } else {
            config.locale = locale;
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return context;
    }

ActivityPreference codes

public class ActivityPreference extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        if (savedInstanceState == null) {
            // Display the fragment as the main content.
            FragmentPreference fragmentPreference = FragmentPreference.newInstance();
            fragmentPreference.setArguments(this.getIntent().getExtras());
            this.getSupportFragmentManager().beginTransaction().replace(R.id.content, fragmentPreference).commit();
        }
    }
}

FragmentPreference codes

public class FragmentPreference  extends PreferenceFragmentCompat {

    public static FragmentPreference newInstance(){
        FragmentPreference fr = new FragmentPreference();
        return fr;
    }

    @Override
    public void onStart() {
        super.onStart();

        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        FragmentPreference.this.getActivity().getWindow().setLayout(width, height);
    }

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

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.preference);
    }
}

Preference Activity with English language

Main menu off app when the language is English

Preference Activity with Farsi language

Main menu off app when the language is Farsi

Mohsen Hatami
  • 317
  • 3
  • 14

0 Answers0