1
votes

I have made 2 different strings.xml, 1 for swedish and 1 for english.

Code for changing Locale

 public void setLocale(String lang) {
    Locale myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(getContext(), BaseActivity.class);
    startActivity(refresh);
}

Onclicklisteners for switching language

swedish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setLocale("sv");
        }
    });
    english.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setLocale("en");
        }
    });

The thing is, the app took it by itself to start the app on swedish, which, as far as I know, haven't set by myself. How do I change the default Locale when the app starts?

Question

  1. How do I set the app to start with using the english xml?
  2. Does anyone have a tip on how to store the choice the user makes? I want it to store if the user presses to use swedish if it closes the app.
3
Have you created two differents values folder for it ? values-sw and values-en and put different strings.xml file in to them.Piyush
@Piyush I have 2 different strings.xml in the values folder. I read something about 2 different values folder but I figured it should work by only adding 2 different string files since that's the only change I want.Viktor
No it can't be worked. You must create two different values folders.Piyush
@Piyush OK, so that's why I can't set the default language? Because it works to change it on the click of a button, but I have to change it everytime I open the app.Viktor
Yes. because of thatPiyush

3 Answers

3
votes

It's just for some languages.

I found when I use translator editor of android studio, to add a locale for example Persian(fa), instead of values-fa android studio add values-fa-rIRso when we asked for Locale("fa") it won't works. So if it doesn't work go to project view and rename your folder (for my example res/values-fa-rIR to res/values-fa).

And setContentView() after that,also

Good luck

0
votes

When user press on language selection store this value into prefrence and once you back again then you have already a stored value in prefrence. now just call the method like:- setLocale_forstartup("en");

`public void setLocale_forstartup(String lang) {

        myLocale = new Locale(lang);
        Locale.setDefault(myLocale);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);

    }`
0
votes

public void changeLanguage(){ try {

        Resources res = mContext.getResources();
        // Change locale settings in the app.
        DisplayMetrics dm = res.getDisplayMetrics();
        android.content.res.Configuration conf = res.getConfiguration();
        conf.locale = new Locale("en");
        // conf.locale = new Locale("en");
        res.updateConfiguration(conf, dm);
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
}