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
 
    