0

Good Morning, I've tried a lot of solution but nothing function in my app. After rotaion, when I start a new activity, it set defaul locale. Please help me, I'me desperate and i'm tried a lot of solution! :( This is my Locale Activity:

    ImageButton inglese = (ImageButton) findViewById (R.id.inglese);    
    ImageButton italiano = (ImageButton) findViewById (R.id.italiano);  

        inglese.setOnClickListener(onClickListener);
        italiano.setOnClickListener(onClickListener);
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(final View v) {
            switch(v.getId()){
                case R.id.inglese:
                     //DO something
                    setLocaleEng();

                break;
                case R.id.italiano:
                     //DO something
                    setLocaleIta();
                break;

            }
        }
    };




private void setLocaleEng() {
    // TODO Auto-generated method stub
    Locale localeEng = new Locale("en");
    Locale.setDefault(localeEng);
    Configuration configEng = new Configuration();
    configEng.locale = localeEng;
    getBaseContext().getResources().updateConfiguration(configEng, getBaseContext().getResources().getDisplayMetrics());
    Intent intent = new Intent(Lingua.this, MainActivity.class);
    startActivity(intent);
    Lingua.this.finish();


}



private void setLocaleIta() {
    // TODO Auto-generated method stub
    // Locale localeIta = new Locale("it");
     Configuration newConfig = new Configuration();
     newConfig.locale = Locale.ITALIAN;
     onConfigurationChanged(newConfig);
        Intent intent = new Intent(Lingua.this, MainActivity.class);
       // Lingua.this.finish();
        startActivity(intent);
        Lingua.this.finish();
}

And this is an activity that extend application:

public class MyApplication extends Application {
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setLocaleEng();
    setLocaleIta();
}

private void setLocaleEng() {
    Locale locale = new Locale("en");
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
          getBaseContext().getResources().getDisplayMetrics());
}



private void setLocaleIta() {
    Locale localeita = new Locale("it");
    Locale.setDefault(localeita);
    Configuration config2 = new Configuration();
    config2.locale = localeita;
    getBaseContext().getResources().updateConfiguration(config2,
          getBaseContext().getResources().getDisplayMetrics());
}

}

Please, help me!!

Ormet
  • 75
  • 12
  • if you don't want to rotate application then add try this in your manifests `android:screenOrientation="portrait"'` Or if you want your app to rotate then store data into `savedInstanceState` Check this (ans)[http://stackoverflow.com/a/6525808/3949810] – Mohit Kacha Feb 28 '17 at 06:52
  • your question is not clear. do you want to keep your data when it is rotated? – Im Batman Feb 28 '17 at 06:54
  • Thanks, for your answers,; I wish that when you rotate the screen the program language does not change. If i set it with a button, for example in english, now when rotate the screen it return to default language (italian) – Ormet Feb 28 '17 at 08:28
  • Possible duplicate of [After the screen rotation, the language of my application will be changed](https://stackoverflow.com/questions/19765527/after-the-screen-rotation-the-language-of-my-application-will-be-changed) – BakaWaii Sep 04 '18 at 01:30

0 Answers0