0

I have a over flow menu from where language is clicked to get singlechoiceitems(list of languages). I have written this code

AlertDialog.Builder languageDialog = new AlertDialog.Builder(
                    MainActivity.this);
            languageDialog.setTitle(R.string.chooseLanguage);
            final String[] languageOptions = { "English", "Nepali" };
            languageDialog.setSingleChoiceItems(languageOptions, 0,
                    new DialogInterface.OnClickListener() {
                        @SuppressLint("NewApi")
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (which == 0) {
                                setLanguage("es");
                            } else if (which == 1) {
                                setLanguage("ne");
                            }
                            dialog.dismiss();
                        }
                    });
            languageDialog.show();
for list of Dailog and 
@SuppressLint("NewApi")
private void setLanguage(String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    android.content.res.Configuration config = new Configuration();
    config.locale = locale;
    this.getResources().updateConfiguration(config,
            this.getResources().getDisplayMetrics());
    MainActivity.this.recreate();
} method to set locale 

The problem is that on screen orientation change the language changes back to device language, why?anything I am missing? I have made changes in anroid manifest file as I read but no solutions worked for me. Any help?

ehehhh
  • 1,066
  • 3
  • 16
  • 27
Dilip
  • 66
  • 10
  • Use lifecycle method of Activity/Fragment and store/Retrieve data using lifecycle methods when orientation is changes. – Rahul Dec 09 '15 at 07:21
  • would you like to give me some examples? – Dilip Dec 09 '15 at 07:35
  • use some sharedpreference to store data at the time of destroy() method and read that in oncreate() by this you can show the saved language. – Rahul Dec 09 '15 at 08:18

4 Answers4

2

Add the following line in AnroidMenifest.xml file in particular activity.

android:configChanges="orientation|layoutDirection|screenSize"
Dhia
  • 10,119
  • 11
  • 58
  • 69
SuN
  • 1,723
  • 1
  • 13
  • 14
0

Yep, unfortunately changing language isn't a thing that Android likes. To make it work you need to call your setLanguage method in EACH activity before onCreate.

0
changing orientation destroy activity and **recreate** it so when it **recreated** it changes to language that set in **oncreate** method.you have to save value in **saveinstatestate** and check on **oncreate** method

if(saveinstatntate!=null)`enter code here`
{
//then set the saved language
}
else
{
//defaultvalue
}
ripan
  • 78
  • 8
  • I am calling the method from alert dailog not inside onCreate method .. look at my code. – Dilip Dec 09 '15 at 07:36
0

I think, you should add in your Android Manifest file:

android:configChanges="locale|orientation"

In your class you should have these imports:

import java.util.Locale; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.res.Configuration; 
import android.content.res.Resources; 
import android.util.DisplayMetrics; 

and the localization code should look like this:

public void setLocale(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, AndroidLocalize.class); 
startActivity(refresh); 
finish();
} 

Please read: How to change language of app when user selects language?

Read also:

Change language programmatically in Android

Set-up the application Language in Android Preferences

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • I have read them and tried didnt work ... what is AndroidLocalize.class here – Dilip Dec 09 '15 at 07:41
  • AndroidLocalize.class: http://stackoverflow.com/questions/20343220/how-to-change-the-language-of-android-app-from-within – piotrek1543 Dec 09 '15 at 07:44
  • In my case its MainActivity .. I have tried with it .. didn't work – Dilip Dec 09 '15 at 07:46
  • Please read them again, it might be problem that you're using both: in AndroidManifest declaration and in class method onConfigurationChange – piotrek1543 Dec 09 '15 at 07:46