3

I search on Google & stackoverflow but I can't solve my problem. I refer http://www.androhub.com/android-building-multi-language-supported-app/ I have the three activities (activity 1,2,3). Activity 1 > Activity 2 > Activity 3 (milti lang change). In Activity 3, I have the two radio button to change (en, hi). After I change language; go to first activity (Activity 1), it is ok. But I want to stay current activity (refresh current activity), after I choose a radio button (changed language). Otherwise, how to recall onCreate() when I go back onBackPressed(). How to do the best way to stay current activity after change language.

onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    txt = (MMTextView) findViewById(R.id.txt);
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

    pref = getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
    editor = pref.edit();

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            String lang = "en";
            switch (i) {
                case R.id.eng:
                    lang = "en";
                    Intent i1 = new Intent(Main3Activity.this, MainActivity.class);
                    i1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i1);
                    finish();
                    break;
                case R.id.hi:
                    lang = "hi";
                    Intent i2 = new Intent(Main3Activity.this, MainActivity.class);
                    i2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i2);
                    finish();
                    break;
                default:
                    break;
            }
            changeLocale(lang);
        }
    });

    loadLocale();
}

changeLocale

private void changeLocale(String lang) {
    if (lang.equalsIgnoreCase(""))
        return;
    Locale myLocale = new Locale(lang); 
    saveLocale(lang);  
    Locale.setDefault(myLocale);    
    Configuration config = new Configuration(); 
    config.locale = myLocale;   
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());  
}

saveLocale & loadLocale

private void saveLocale(String lang) {
    editor.putString("save", lang);
    editor.commit();
}

private void loadLocale() {
    String lang = pref.getString("save", "");
    changeLocale(lang);
}
Shwe
  • 41
  • 1
  • 4
  • This link may help you: https://stackoverflow.com/questions/8049207/how-to-refresh-activity-after-changing-language-locale-inside-application?answertab=oldest#tab-top – John Le Sep 10 '17 at 15:16
  • @Shwe Please check out this link ---> https://stackoverflow.com/questions/45088039/set-different-language-in-android-studio/45088145#45088145 – yash786 Sep 10 '17 at 16:32

4 Answers4

2

Use recreate( ) to restart the current activity.

sam
  • 1,800
  • 1
  • 25
  • 47
0

You can close and open the same activity like:

startActivity(new Intent(ActivityName.this, ActivityName.class));
finish();

I hope this works.

Cahid Enes Keles
  • 733
  • 1
  • 8
  • 13
0

Call the below function when the user choose different language

 public void setApplicationLanguage(String language) {
    Locale myLocale = new Locale(language);
    Resources res = getBaseContext().getResources();
    DisplayMetrics display = res.getDisplayMetrics();
    Configuration configuration = res.getConfiguration();
    configuration.locale = myLocale;
    res.updateConfiguration(configuration, display);
    Intent refresh = new Intent(getBaseContext(), ChooserActivity.class);
    getBaseContext().startActivity(refresh);
}
Bahaa Hany
  • 744
  • 13
  • 22
-2
/***You need to create separate String file for both arabic and 
English***/

English
Values
String.xml
Hindi   
Values-hi
String.xml

/**You can change the application language using the following code:**/

language =  "en"; or  language =  "hi";

@Override
public void recreate()
{
if (android.os.Build.VERSION.SDK_INT >= 14)
{
    super.recreate();
}
else
{
    startActivity(getIntent());
    finish();
}
}

private void updateLanguage(String language)
{
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",language);
editor.apply();

recreate();
}
yash786
  • 1,151
  • 8
  • 18