I am trying to change app localization at runtime it working correctly but when clear app from memory and reopen it again I find that resources like strings.xml and styles.xml take the default mobile language!
            Asked
            
        
        
            Active
            
        
            Viewed 94 times
        
    0
            
            
        - 
                    So it doesn't work the second time app is opened? – Vedprakash Wagh Jun 23 '19 at 09:03
- 
                    yes, when I close the app and reopen it again it not working. – Mina George Jun 23 '19 at 09:25
- 
                    how are localizing your app? Are you using something like shown here?https://stackoverflow.com/questions/2900023/change-app-language-programmatically-in-android/35149135 How are you storing the locale? Are you using SharedPreferences? – Vedprakash Wagh Jun 23 '19 at 09:43
- 
                    yes, I do like this. – Mina George Jun 23 '19 at 10:44
- 
                    Okay, but are you using SharedPreference to store the locale? – Vedprakash Wagh Jun 23 '19 at 10:47
- 
                    yes I am storing it using SharedPreference – Mina George Jun 23 '19 at 10:48
- 
                    Okay, maybe you're missing something? I'm localizing my app doing the same, but it doesn't reset itself. I'll write my method in a few minutes, check to see if it works for you. – Vedprakash Wagh Jun 23 '19 at 10:52
- 
                    okay, Thank you. – Mina George Jun 23 '19 at 10:58
1 Answers
0
            See my method to localize the app:
I created an abstract class like below:
public abstract class BaseLocalization extends AppCompatActivity {
    SharedPreferences preferences;
    Constants constants;
    String currentLocale = "en";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        constants = new Constants();
        preferences = getSharedPreferences(constants.getPreferences(), MODE_PRIVATE);
        currentLocale = preferences.getString(constants.getLocale(), "en");
        setLocale(currentLocale);
    }
    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);
    }
}
After doing this, I made all the classes which extended AppCompatActivity extend this class. And saved the locale in SharedPreferences. And the locale is persisted until the app data is cleared/uninstalled. 
 
    
    
        Vedprakash Wagh
        
- 3,595
- 3
- 12
- 33
