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
1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I am using all this. As I said, Its working well in android emulator. I am looking out for problem as describe in question.
who to identify which the language you want to set?
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
from where you have called setLocale method?
This answer has blatantly been copied from here. Without explanation funny! Original one here stackoverflow.com/a/40849142/1149398

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.