2

I am new to android app development. I am working on app where language can be changed. Example: English to Hindi or Kannada. Language change working fine on emulator. But when I am changing language on android phone its not changing unless I change languages from Settings to that particular language.

I wanted to dynamically change language in app itself instead of going to settings. Is this possible or we have to go by above way only?

P.S. Android phone has 7.0 Nougat

Any help would be much appreciated. Thanks.

This is what I have wrote.

    private void setLocale(String lang){
    Locale locale=new Locale(lang);
    locale.setDefault(locale);
    Resources resources = getResources();
    Configuration configuration = resources.getConfiguration();
    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
        configuration.setLocale(locale);
    } else{
        configuration.locale=locale;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
        getApplicationContext().createConfigurationContext(configuration);
    } else {
        resources.updateConfiguration(configuration,displayMetrics);
    }
    //save data to sharedPreferences
    SharedPreferences.Editor editor=getSharedPreferences("Settings",MODE_PRIVATE).edit();
    editor.putString("My_Lang",lang);
    editor.apply();
}

    public void showChangeLanguageDialog()
{
    final String[] listItems={"हिंदी","ಕನ್ನಡ","मराठी","தமிழ்","اردو","English"};

    AlertDialog.Builder mBuilder=new AlertDialog.Builder(MainActivity.this);
    mBuilder.setTitle("Change Language..");
    mBuilder.setSingleChoiceItems(listItems, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which)
            {
                case 0: //Hindi
                    setLocale("hi");
                    recreate();
                    break;
                case 1://Kannda
                    setLocale("kn");
                    recreate();
                    break;
                case 2://English
                    setLocale("en");
                    recreate();
                    break;
            }
            //dismiss dialog when language selected
            dialog.dismiss();
        }
    });

    AlertDialog mDialog=mBuilder.create();
    //show create dialog
    mDialog.show();
}
//showChangeLanguageDialog is called on button click
Darshan Adakane
  • 87
  • 2
  • 14
  • 2
    Possible duplicate of [How to change language of app when user selects language?](https://stackoverflow.com/questions/12908289/how-to-change-language-of-app-when-user-selects-language) – Santanu Sur Apr 28 '18 at 10:49

1 Answers1

1

Set the local language in your BaseActivity in attachBaseContext

    public class BaseActivity extends AppCompatActivity {

             @Override
                protected void attachBaseContext(Context newBase) {
                    String lngCode=PreferenceManager.
getDefaultSharedPreferences(context).getString(CURRENT_LANGUAGE_CODE, Locale.getDefault().getLanguage())
                    Locale newLocale = new Locale(lngCode);

                    Context context = ContextWrapperLanguage.wrap(newBase, newLocale);
                    super.attachBaseContext(context);
                }
            }

Set language in Preferences like en,ar,it

and when you change the language don't forgot to restart the application.

MJM
  • 5,119
  • 5
  • 27
  • 53
  • I am using all this. As I said, Its working well in android emulator. I am looking out for problem as describe in question. – Darshan Adakane Apr 28 '18 at 11:04
  • who to identify which the language you want to set? – MJM Apr 28 '18 at 11:05
  • in above example i have used default language as `Locale.getDefault().getLanguage()` so it define default language from device,if you not change any language in preferance – MJM Apr 28 '18 at 11:09
  • from where you have called `setLocale` method? – MJM Apr 28 '18 at 11:34
  • This answer has blatantly been copied from here. Without explanation funny! Original one here https://stackoverflow.com/a/40849142/1149398 – sud007 Sep 24 '21 at 16:32