2

I want to change the language in all activity. Is there any simple way to change the language without using string resource.I have tried something like below code,but not works.Any one help me to change the language in all activity by simple way?

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView arg0, View arg1,
                                   int arg2, long arg3) {
            Configuration config = new Configuration();
            switch (arg2) {
                case 0:
                    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(LocalizationUpdaterActivity.this);
                    preferences.edit().putString("lang", "ar").commit();
                    break;
                case 1:

                    break;

                default:
                    config.locale = Locale.TAIWAN;
                    break;
            }

        }

        public void onNothingSelected(AdapterView arg0) {
            // TODO Auto-generated method stub

        }
    });

Application code:

    public class MainApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String lang = preferences.getString("lang", "en");
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
    }
}
Androider
  • 3,833
  • 2
  • 14
  • 24
Suganya Rajasekar
  • 685
  • 3
  • 14
  • 37

1 Answers1

0

Basically your code is correct.

The following code works. Check if you haven't missed something. I guess your application code is not executed or you string translations are not available.


Working Sample:


Application:

@Override
public void onCreate() {
    super.onCreate();
    setResourceLocale(new Locale("en"));
}

private void setResourceLocale(Locale locale){
    if(Build.VERSION.SDK_INT >= 17){
        getBaseContext().getResources().getConfiguration().setLocale(locale);
    }else{
        Configuration config = getResources().getConfiguration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,getResources().getDisplayMetrics());
    }
}

Activity:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i(TAG, getString(android.R.string.no));
}

AndroidManifest.xml:

Make sure you added your custom Application class!

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name=".MainApplication"
    >

    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Addition:

To use the Language with Country Code use:

new Locale("de","DE")

Check out this SO answer for the correct codes.

Community
  • 1
  • 1
daemmie
  • 6,361
  • 3
  • 29
  • 45