I need to change the language of my app, that is, I have an activity where I have a list of languages to choose. When I select one I change it, but I'm using some methods that can only be used with android KItKat
4.2 and on and I want it to be used from 2.3. Here is the code:
public void cargarLocale() {
String PrefIdioma = "idioma";
String idioma = leePreferencias (PrefIdioma, this);
cambiarLocale(idioma);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public void cambiarLocale(String locale) {
//Si está vacío, queda el idioma por defecto del dispositivo
if (Objects.equals(locale, ""))
return;
//Configuramos el idioma cargado desde SharedPreferences
Locale miLocale = new Locale(locale);
Locale.setDefault(miLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = miLocale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
and the method that the language selects me is the following:
findViewById(R.id.ingles).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nuevoLocale = "";
nuevoLocale = "en";
Toast.makeText(Settings.this, "Selected English language !", Toast.LENGTH_LONG).show();
salvarLocale(nuevoLocale);
}
});
public void salvarLocale(String locale) {
String PrefIdioma = "idioma";
salvaPreferencias(PrefIdioma, locale, this);
Intent i = getBaseContext().getPackageManager() .getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
}