G.V.Raghuram.
G.V.Raghuram.

Reputation: 29

Android Locale Change

I am trying to change locale of my android app by using spinner the spinner contains list of languages intial value in spinner would be English but when the activity gets loaded the activity gets intented repeatly .. if i set the first value as select then it working perfectly but i need the intial value of the activity to be in English. please help me on this issue.

Below is my code for the language spinner

Position 0 is english Position 1 is hindi

   language.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            // TODO Auto-generated method stub
            if(pos==0){
                languagechange("en");

            }
            if(pos==1)
            {
                languagechange("hi");
            }

        }

And the locale change code is below

    public void languagechange(String lang) {

    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(this, LoginActivity.class);
    startActivity(refresh);
    LoginActivity.this.finish();
}

Upvotes: 2

Views: 494

Answers (1)

Fedy Venom
Fedy Venom

Reputation: 399

"initial value of the activity to be in English"

Locale myLocale = new Locale("en");
Configuration conf = new Configuration();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    conf.locale = myLocale;
    getResources().updateConfiguration(conf, null);
    setContentView(R.layout.activity_main);
}

Put it before setContentView(). Hope this solve your issue.

Upvotes: 1

Related Questions