set different language in android studio

前端 未结 2 1521
梦毁少年i
梦毁少年i 2021-01-17 04:21

am new in android studio and I am trying to make my simple app weather info via yahoo weather service JSON data , i would like to make a different language display in my app

2条回答
  •  無奈伤痛
    2021-01-17 04:46

    /***You need to create separate String file for both arabic and English***/
    
    English
     Values
      String.xml
    Arabic   
     Values-ar
      String.xml
    
    /**You can change the application language using the following code:**/
    
     language =  "en"; or  language =  "ar";
    
    @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();
    }
    

提交回复
热议问题